-
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
refetchQueries after mutation #1900
Comments
From the documentation:
Also you will have to convert your It should look like this: import gql from 'graphql-tag'
const USER_QUERY = gql`
query currentUser {
id
login
}
`
const LOGIN_MUTATION = gql`
mutation login {
login(login: "foobars")
}
`
[...]
mutate({
mutation: LOGIN_MUTATION,
variables: {
...values
},
refetchQueries: [`currentUser`]
})
|
I'm using graphql-loader and writing mutations and queries in separate graphql files. How should I approach this way? BTW I've tried to use gql and use the name of the query, but it doesn't work as well. |
then you should probably create a reproduction with https://github.com/apollographql/react-apollo-error-template . I never used refetchQueries so my knowledge is limited to the documentation 😞 |
See also: #1697 where I tried to figure out the same problem. Did not find a really good solution yet but it may give you some insight in the options. |
@tokenvolt I think this may be a shape issue within refetchQueries. Can you try
The object is needed in order to support things like variable changes |
@jbaxleyiii still no luck |
@tokenvolt do you think you could create a simple reproduction for this? We have an error template and code sandbox listed in the issue template (try to create a new issue and it should be there) or a failing test? |
@jbaxleyiii I'll try to reproduce |
@tokenvolt FYI I was also not lucky with using the query-name based variant of In principle though, the query-name based
|
Hey, guys, I figured out that it was an issue in my application. Sorry for bothering, as it was just a stupid mistake on my side. But I am too experiencing a race condition #1821 |
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! |
For those who are still having this problem, try putting
|
@abraga still doesn't work for me |
Hey I'm experiencing same problme, any solutions?? none of the things mentioned above solved the issue. Everything is working fine, but refetchQueries |
Same issue here - I can manually call |
@wesbos Hi Wes how you doing? I had to use a workaround here, I imagine you are using a state container(Redux, Mobx), so you have the records collection in your store and every time you make a mutation, select all the objects fields, once the response comeback you can use that data to merge it into your existing records collection. Do you catch the idea? |
I'm not using any sort of state container outside of Apollo - so I'm not sure this applies |
I was having a similar issue trying to refetch a query after my login mutation. But I placed some console.logs and it looks like in my issue it may just be an async issue. My mutation is firing first but the query is firing before my result gets back so it makes the query i am firing appear to have no change in my UI. I tried simply wrapping my resolver in async/await but of course it was not that easy :P |
So did you figure it out? |
@wesbos My query is taking my userId through apollo's context object. I believe that the query is firing before I can set the context so it still sees context.userId as undefined and so my query appears to be the same as when my user was logged out. If I run the query again it updates as it is supposed to because now that my user is logged in the context is set. I am in the middle of trying to figure it out but I believe apollo-link-context is what I am looking for but no I have not solved it yet. I am not sure if you are using context or not but my refetch works fine otherwise so I would say this may be an async issue for you as well but I am in the same boat as you so who knows :P |
So I ended up solving my issue for the most part. I needed to wrap my login resolver to return a Promise. What was happening is my context was being updated before I received my login token. If your refetch queries is firing before your login process is finished than the query will appear to not change. I am not sure if this helps but I wanted to share what I found. |
works well when you use optimisticResponse :( |
after a while, i used the reply of @abraga like a started point, and search in other issiues and found that, the way that i be able to solve this problem was the code that @abraga supply was
Instead, I used:
I'm using "withApollo" HOC and graphql as an decorator. |
Guys, just a heads-up: When using React, if you call ...
export default graphql( my_mutation, { name: 'my_mutation', options: {refetchQueries: [ 'queryToRefetch']} } ) ( MyComponent) |
Use a Map to pass the limitation mutate({
.........,
refetchQueries: new Map([[0,'todolist'],[1, 'todogroups']])
})
function bypass(...queryNames){
return new Map(queryNames.map((name, i) => [i, name])
}
mutate({
.........,
refetchQueries: bypass('todolist', 'todogroups')
}) |
I'm having an issue re-rendering the component. 1- I have an index showing basically all my bank accounts (it uses bank account query) |
This is absolutely not obvious what is expected. Their documentation is miserable. |
What How can you specify
|
Experiencing a similar situation as @waclock. I see that new data is being fetched, but the component depending on that query doesn't update. |
Hi guys!
Hope it helps. |
So far there's been like 8 different methods posted, most of which have never worked for me. It's completely ridiculous that it's such a game of whispers (does it change in every version?). FWIW, in my case I was using
That works for me. None of the other posted solutions did anything. |
This is complete nonsense and yes @MitchEff 's solution works: await client.mutate({
mutation: COMPLETE_CHALLENGE,
variables: data,
refetchQueries: () => [{query: UNCOMPLETED_CHALLENGES}]
}) |
I get error back of You must wrap the query string in a "gql" tag. when I add the query name.
|
ok so passing the entire query seems to be accepted (no error) but also my data is still old. is there another promise I need to process on to wait for the second query to process before I pass to he result to the calling method?
right now I do mutate {blah blah).then(stuff) is there now a new var passed to then to tell me when the refresh has occurred or will it only pass me stuff for the then after it's mutated AND refreshed the cache. Thanks for any help. Update: it seems to work after the second call (the first call looks like nothing happens, but click update again and it works fine.) Any advice , as it always seems one record behind.? |
Solved: had to add awaitRefetchQueries = true to my options array. |
Any idea how do we get the response data from Mutation
Query
Usage
Output
But I expect a response data from |
Still experiencing this issue. I've tried the solutions listed with no success. Will be diving into the race issue listed as I have yet to explore that potential. Here is where I'm at with no luck:
|
Maybe this helps: When I'm loading the query from a .graphql file via graphql.macro the refetch doesn't work.
I wanted to recreate it with codesandbox but I can't find any public graphQL API, where I can write data. |
How did you get "refetchQueries" response |
I've been wrestling with getting the Apollo client to wait for the queries specified in For one, you need the For example, given a mutation a call like this: await client.mutate({
mutation: MUTATION,
variables: data,
refetchQueries: ['QueryName'],
awaitRefetchQueries: true
}) The |
worked. |
My solution to refetch using variables on Apollo 3.0:
See more about the fetch policy here. |
Hi @hbw3 , sorry for the big delay, but i hope this post helps other people that have the same question as you. All refetch queries are managed as normal queries, so we have access to all methods as if we are calling a individual one. You can check this in the documetation. So, a example for this is:
As u can see, in onCompleted we have access to the result of refetchQuery and we can use this to do whatever we want. Same with the onError function. |
If anyone runs into this same issue, what fixed it for me since I've never had any issues with refetchQueries before, was this: I had a loop over a child collection, and was then fetching the parent in a refetchQueries when a child was deleted. Setting the id or the child as key somehow made a difference... {data.rules_RuleSet &&
data.rules_RuleSet.rules.map((rule, i) => <Rule rule={rule} key={rule.id} ruleSetId={ruleSetId} />)} |
The thing that fixed it for me was changing the const { data, error } = useGetProductsQuery({ |
Hi @tokenvolt , I wanted to ask one question if we have fire any useMutation after that refetchQueries I want to display that data into the UI how to do that.
I want to print current User in the screen |
I am still having the issue, I am getting null value in data result but the same query is returning value in POSTMAN app. Is there anyone who faced the same issue , please help
Response: |
Intended outcome:
I'm trying to refetch some queries after the mutation and I expect them to be executed.
Actual outcome:
refetchQueries are not working as they are described in the docs
How to reproduce the issue:
LOGIN_MUTATION and USER_QUERY are strings in graphql syntax. I don't see USER_QUERY gets executed after the mutation.
The text was updated successfully, but these errors were encountered: