【问题标题】:Apollo - GraphQLError: Syntax Error: Expected $, found NameApollo - GraphQLError:语法错误:预期 $,找到名称
【发布时间】:2020-04-18 02:27:53
【问题描述】:

我在 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:语法错误:应为 $,找到名称“单位”

【问题讨论】:

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


    【解决方案1】:

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

    module.exports.Mutation = {
        createUnit
    }
    

    要在 graphql 游乐场中使用它,您只需指定要在突变中使用的变量,如下所示

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

    并在变量中添加:

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

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 2019-04-05
      • 2019-11-12
      • 2022-01-15
      • 2019-01-23
      • 2019-10-09
      • 2021-11-10
      • 2021-07-28
      • 2023-03-31
      相关资源
      最近更新 更多