-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update store after mutation (create) unclear how to handle #1697
Comments
in my case the update function doesn't gets called. |
Got some interesting feedback on Slack which I think should be added: As far as I understand for now I would have to do this part for EVERY query?
So every time I do a query on the same nodes I would have to change all update calls? So all code becomes strongly dependent on each other? That makes all code hardcoded connected? For example: In my "View account" component I cannot see my total amount of unpaid invoices unless I hardcode a writeQuery in my invoices list to update that component? |
Not a "proper" solution, but I have given up on attempting smart cache busting after running mutations. I just decorate the component with |
While totally agreeing that this should not be needed it sounds like a very simple and effective fix for the time being! Another alternative is just polling: http://dev.apollodata.com/react/api-queries.html#graphql-config-options-pollInterval export default graphql(gql`query { ... }`, {
options: { pollInterval: 5000 },
})(MyComponent); Even easier because you can implement it without any callback. |
You can get all the invoices and then filter those that are paid. Then you can use the mutation recommended above with readQuery and writeQuery helpers. |
Thanks for the feedback @jurajkrivda ! I think if I understand you well:
That's the logic? While I would hope that it seems duplicated logic (I already wrote that query) and very cumbersome if you get additional parameters as it will become a matrix? Let's say I have 3 boolean parameters that's already 2 * 2 * 2 = 8 manual filters I have to implement to update the data? If I have more it becomes a huge amount? |
This issue has been automatically marked as stale becuase it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Apollo Client! |
@probot-stale[bot] actually this issue is still open and wondering whether there are any new strategies which have been developed recently. |
@lucfranken first of all I believe that this is a duplicate of (or at least related with) apollographql/react-apollo#708 In fact I ran into exactly the same issue and ended up calling all queries in the I was hoping that I could at least simplify my case by using So for now I'm stuck with the repeated store updates which works but really adds a lot of logic and code while I would find a simple refetch of all queries that are already in the store to be a good compromise between maintainability and client-side performance. Edit: Oh, and as you mention in your last comment @lucfranken, it also only works for me because I currently have queries with one boolean parameter. As you mention this would soon become impossible for more complex parametrizations (think: pagination where you don't actually know the total number of pages)… |
@ctavan yes party it's related but this issue actually was created based on the recommendations in the documentation that the refetchQueries would be deprecated. Input from the Apollo team on the right direction is needed. |
This issue has been automatically marked as stale becuase it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Apollo Client! |
It would be helpful if there is an API like |
This issue has been automatically marked as stale becuase it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Apollo Client! |
This issue has been automatically closed because it has not had recent activity after being marked as stale. If you belive this issue is still a problem or should be reopened, please reopen it! Thank you for your contributions to Apollo Client! |
Hi, is there any updates on this issue? So now I have to either:
Is that right? |
I had experience a situation like this where i needed to update an item from a filtered list and i handled it using the componentWillReceiveProps lifecycle method. Example
|
Why the Apollo team is ignoring this. It is a to common problem and they are no doing anything about it. Their may be maybe a way but there is nothing document about it really dissapointing |
It seems that refetchQueries is not deprecated anymore. All docs are shuffled around so it's not easy to find back the early mentions of this but it seems changed in the docs. |
How am I supposed to know which argument I should use with refetchQueries ? refetchQueries doesn't seem to be a solution either ... |
@DarKDinDoN I'd prefer a better solution using <Mutation mutation={CREATE_FOO} refetchQueries={["allFoos"]}>
|
@renato yes but am I supposed to know which argument I should provide to the query ? Especially, when the parameters are dynamic. |
@DarKDinDoN I try it all I get your point their is no way you can know all the multiple dynamic queries that are being generated by the users. I still don't know why if is such a common thing they don't provide a way to refetch ALL. Anyway if works for something my solution was to Flush the whole cache, I know it's not a nice solution but works for me since I don't expect user will be adding and removing to much elements anyway. Also cache refresh more often that we think like for example if users recharge the page so it is no that big o a deal. Here is how can you implement this. ` export default withApollo(YOURCOMPONENT); //this will give you access to the client as a prop so inside your component, after the add or delete action you just call this.props.client.resetStore(); |
@minardimedia one issue I have with resetStore is it includes reseting my auth/user queries, which causes mayhem in apps that have an "authenticated" area (where user would get kicked back to login or content would flicker). |
Hi @acomito I just came to know about this repo apparently it works for this issue, I haven't try it but you can take a look at it (apollo-link-watched-mutation) https://github.com/haytko/apollo-link-watched-mutation |
@yashwp thanks, your example covers a very simple example. Try it when ALL_COURSES has many filters/parameters. |
@yashwp Thanks! Tried it out on my own models and it works! very simple and nice code :) |
Why is this still marked as closed? Clearly there hasnt been a satisfactory resolution to this issue. Can we disable the bot for this issue? |
sooooo? |
????? |
Chiming in, but without a solution. Agree with the rest of you that mutation insertions on cached + paginated + sorted queries feels like a pretty common use-case, and it would be wonderful to see a more formal solution for this in Apollo. |
I've created a package in an attempt to solve these issues and make complex cache updates as painless as possible. https://www.npmjs.com/package/apollo-augmented-hooks See https://github.com/appmotion/apollo-augmented-hooks/#--modifiers and the linked caching guide in particular. I've been using this in production for a while now and just made it open source so other people can benefit from it as well. |
There are a lot of docs but one part seems to be totally unclear on how to approach.
I use GraphCool as backend and React Native as frontend so that's how the names of the queries are setup.
Say I have a list of invoices
I have 2 queries (I show 2 lists on the screen): All & unpaid
I insert a new invoice:
I do understand that Apollo is not magic in a sense that it would automatically update my 2 views: all & unpaid. The advice in the documentation at this moment seems to be that I should use: update()
So I do that:
The problem: The All invoices list will get updated. The one with the paid filter does not.
It's totally unclear how to get this right. I even tried adding variables to the writeQuery and readQuery but it just doesn't seem to work out. The simple examples in the tutorials DO work but it seems the more complex cases don't.
Questions:
The text was updated successfully, but these errors were encountered: