我在ui / playground中使用 GraphQL ,语法如下,效果很好。
mutation {
createUnit(unit: {name: "hehehe great!!!"}) {
id,
name
}
}
我的疑问是,如何在Apollo Client中使用GraphQL,我发现了类似的内容:
mutation createUnit(unit: {name: String!}) {
createUnit(unit: {name: "looks great!!!"}) {
id,
name
}
}
但不幸的是,我得到了错误:
GraphQLError :语法错误:预期的$,找到名称“ unit”
答案 0 :(得分:2)
确保已定义类型Unit
并将createUnit
类型添加为解析器中的突变
module.exports.Mutation = {
createUnit
}
要在graphql操场上使用它,只需指定要在此类突变中使用的变量
mutation($unit: Unit){
createUnit(unit: $unit){
id
name
}
}
,然后在变量中添加:
{
"unit": {
name: "looks great!!!"
}
}