I am trying to make a mutation
call to my graphQL server from a react application. The react code looks like the following:
client.query({
query: gql`
mutation{
addTeam(input:{name:"Somename", label:"somelabel"})
{error, status}
}`
}).then((resp: any) => {
console.log("Success", resp);
}).catch(err => {
throw err;
})
And I am getting the following error:
But if I change the same request, from mutation
to query
, and make the necessary changes in my node-graphQL-server to handle it as query
instead of mutation
the same code works.
Mutation
docs saysIn GraphQL, mutations are identical to queries in syntax, the only difference being that you use the keyword
mutation
instead ofquery
...
Oh and BTW, the same mutation
query WORKS in Playground
.
Please help guys, my work is kinda stopped coz of this issue.
Thanks!
You should use the mutate
method of the client for mutations, not the query
method. The options for the method can be found in the docs. Apollo is opinionated about how queries and mutations are treated, so each method has different options that are appropriate to each operation's behavior (for example, mutate
includes a refetchQueries
option).
client.mutate({
mutation: gql`
mutation {
addTeam(input:{name:"Somename", label:"somelabel"}) {
error
status
}
}`,
})