Apollo-GraphQLError:语法错误:预期的$,找到名称

时间:2019-12-28 15:31:32

标签: graphql react-apollo apollo-client graphql-js

我在ui / playground中使用 GraphQL ,语法如下,效果很好。

    mutation {
      createUnit(unit: {name: "hehehe great!!!"}) {
        id,
        name
      }
    }

inserir a descrição da imagem aqui

我的疑问是,如何在Apollo Client中使用GraphQL,我发现了类似的内容:

mutation createUnit(unit: {name: String!}) {
       createUnit(unit: {name: "looks great!!!"}) {
                            id,
                            name
                        }
                    }

但不幸的是,我得到了错误:

GraphQLError :语法错误:预期的$,找到名称“ unit”

1 个答案:

答案 0 :(得分:2)

确保已定义类型Unit并将createUnit类型添加为解析器中的突变

module.exports.Mutation = {
    createUnit
}

要在graphql操场上使用它,只需指定要在此类突变中使用的变量

mutation($unit: Unit){
  createUnit(unit: $unit){
    id
    name
  }
}

,然后在变量中添加:

{
  "unit": {
    name: "looks great!!!"
  }
}