GraphQL 突变:不变违规:必须包含查询定义
GraphQL mutation: Invariant Violation: Must contain a query definition
我正在尝试从 React 应用程序对我的 graphQL 服务器进行 mutation
调用。反应代码如下所示:
client.query({
query: gql`
mutation{
addTeam(input:{name:"Somename", label:"somelabel"})
{error, status}
}`
}).then((resp: any) => {
console.log("Success", resp);
}).catch(err => {
throw err;
})
我收到以下错误:
但是如果我将相同的请求从 mutation
更改为 query
,并在我的 node-graphQL-server 中进行必要的更改以将其处理为 query
而不是 mutation
相同的代码有效。
Apollo-Client Mutation
docs says
In GraphQL, mutations are identical to queries in syntax, the only difference being that you use the keyword mutation
instead of query
...
哦,顺便说一句,相同的 mutation
查询在 Playground
中有效。
请大家帮忙,因为这个问题我的工作有点停止了。
谢谢!
目前有一个 GitHub 问题提到了这个错误:https://github.com/apollographql/apollo-client/issues/1539
您应该使用客户端的 mutate
方法进行突变,而不是 query
方法。可以找到该方法的选项 in the docs。 Apollo 对如何处理查询和变更有自己的看法,因此每个方法都有适合每个操作行为的不同选项(例如,mutate
包含一个 refetchQueries
选项)。
client.mutate({
mutation: gql`
mutation {
addTeam(input:{name:"Somename", label:"somelabel"}) {
error
status
}
}`,
})
我正在尝试从 React 应用程序对我的 graphQL 服务器进行 mutation
调用。反应代码如下所示:
client.query({
query: gql`
mutation{
addTeam(input:{name:"Somename", label:"somelabel"})
{error, status}
}`
}).then((resp: any) => {
console.log("Success", resp);
}).catch(err => {
throw err;
})
我收到以下错误:
但是如果我将相同的请求从 mutation
更改为 query
,并在我的 node-graphQL-server 中进行必要的更改以将其处理为 query
而不是 mutation
相同的代码有效。
Apollo-Client Mutation
docs says
In GraphQL, mutations are identical to queries in syntax, the only difference being that you use the keyword
mutation
instead ofquery
...
哦,顺便说一句,相同的 mutation
查询在 Playground
中有效。
请大家帮忙,因为这个问题我的工作有点停止了。
谢谢!
目前有一个 GitHub 问题提到了这个错误:https://github.com/apollographql/apollo-client/issues/1539
您应该使用客户端的 mutate
方法进行突变,而不是 query
方法。可以找到该方法的选项 in the docs。 Apollo 对如何处理查询和变更有自己的看法,因此每个方法都有适合每个操作行为的不同选项(例如,mutate
包含一个 refetchQueries
选项)。
client.mutate({
mutation: gql`
mutation {
addTeam(input:{name:"Somename", label:"somelabel"}) {
error
status
}
}`,
})