I'm working on an Angular project with Apollo and GraphQL. I have a mutation that updates a list of accounts with relevant details associated with it. After the successful mutation, I am using refetchqueries to query the API for updated list of accounts. Everything works till this part.
this.apollo.mutate({
mutation: mutationCreateNewAccount,
variables: {
accountNumber: this.accountNumber,
accountType: this.accountType,
routingNumber: this.routingNumber,
nameOfAcountHolder: this.name
},
refetchQueries: [{
query: queryAccounts,
variables: { accountNumber: this.accountNumber }
}]}).subscribe(({ data }) => console.log(data),
The 'data' for the subscription returns response from the mutation but is there a way I could use the data returned by 'queryAccounts' which is also run as part of this mutation?
There seems to be a way to do this in react but I was unsuccessful to do something similar in Angular.
If a mutation updates a single existing entity, Apollo Client can automatically update that entity's value in its cache when the mutation returns. To do so, the mutation must return the id of the modified entity, along with the values of the fields that were modified.
The useMutation React hook is the primary API for executing mutations in an Apollo application. As shown above, you use the gql function to parse the mutation string into a GraphQL document that you then pass to useMutation . Unlike useQuery , useMutation doesn't execute its operation automatically on render.
A function that performs an asynchronous task and returns a promise.
You mentioned there are ways to do it in react and which I don't think is true.
Let me explain how mutations work in Apollo.
apollo.mutate()
with refetchQueries
optionconsole.log(data)
)refetchQueries
starts a new HTTP request and runs against the server.I explained it here, in docs: https://www.apollographql.com/docs/angular/features/cache-updates.html#after-mutations
Basically, refetchQueries is not to fetch or access data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With