Reputation: 25
I want to fetch the userUnpurchasedCourses query after performing the addCourseToOrder mutation but I am unable to do that. After adding one class to the order, it fetches the userUnpurchasedCourses query but if I again add the course to order then the query won't run.
I have tried to perform the query in the then block of mutation and also tried the refetch queries but the problem with refetch query is that I am not able to get the result of the query.
this is how I define the mutation
const Mutation = async (query, variables, refetch) =>
client.mutate({
mutation: gql`
${query}
`,
variables,
refetchQueries: () => [
{
query: gql`
${refetch?refetch:query}
`,
variables
}
]
});
Here I used mutation like this
let res = await Mutation(classes.addCourseToOrder(), {
userId,
id,
orderId,
amount,
})
if (res.data.addCourseToOrder) {
this.props.getUserUnpurChasedCourses({id})
}
getUserUnpurChasedCourses will be responsible to run the userUnpurchasedCourses.
If you need more detail than please let me know
Thanks in advance.
Upvotes: 1
Views: 1395
Reputation: 25
As I observe the issue I find that the query is running every time after the mutation but instead of fetching the data from the server(Network) it is fetching the data from the cache memory that's why even after checking the network tab of browser console I was not getting any request for it which made me unable to pinpoint the issue but I was getting the result from the previously-stored result then after googling a lot I find that there is a way to disable the fetch from cache memory so I tried it over and over again until I get it done.
all I did is changed the query from this
export const Query = async (query, variables) =>
client.query({
query: gql`
${query}
`,
variables
});
to this
export const Query = async (query, variables) =>
client.query({
query: gql`
${query}
`,
variables,
fetchPolicy: 'network-only'
});
Upvotes: 1