Apollo - GraphQLError: Syntax Error: Expected $, found Name

Apollo - GraphQLError: Syntax Error: Expected $, found Name

我在 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"

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

module.exports.Mutation = {
    createUnit
}

要在 graphql playground 中使用它,您只需像这样指定要在突变中使用的变量

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

并在变量中添加:

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