【问题标题】:GraphQL: Unknow directive includeGraphQL:未知指令包括
【发布时间】:2021-02-07 13:52:23
【问题描述】:

我正在练习在 Nodejs 中使用带有 Graphql 的指令。在 Playground 中我想使用 @include 指令时,Graphql 客户端告诉我:“未知指令包含”。这是我的代码:

query getChar($id: ID!, $withGames: Boolean!) {
  getCharacter(_id: $id) {
    _id
    name
    race
    games @include(if: $withGames) {
      _id
      title
    }
    image
  }
}

在我的查询变量中:

{
  "id": "5f945a7b24c84f35ecfae633",
  "withGames": false
}

但我收到以下错误:

{
  "error": {
    "errors": [
      {
        "message": "Unknown directive \"@include\".",
        "locations": [
          {
            "line": 6,
            "column": 11
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED",
          "exception": {
            "stacktrace": [
              "GraphQLError: Unknown directive \"@include\".",
              "    at Object.Directive (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\validation\\rules\\KnownDirectivesRule.js:56:29)",
              "    at Object.enter (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\language\\visitor.js:323:29)",
              "    at Object.enter (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\utilities\\TypeInfo.js:370:25)",
              "    at visit (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\language\\visitor.js:243:26)",
              "    at Object.validate (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\validation\\validate.js:69:24)",
              "    at validate (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\apollo-server-core\\src\\requestPipeline.ts:510:14)",
              "    at Object.<anonymous> (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\apollo-server-core\\src\\requestPipeline.ts:296:32)",
              "    at Generator.next (<anonymous>)",
              "    at fulfilled (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:5:58)",
              "    at processTicksAndRejections (internal/process/task_queues.js:97:5)"
            ]
          }
        }
      }
    ]
  }
}

我做错了什么?

【问题讨论】:

    标签: graphql graphql-js graphql-tools


    【解决方案1】:

    It was a version problem of the graphql-tools。检查node_modules/graphql-tools/package.json 文件,我发现我使用的是版本4。我发现here 版本4 的指令存在问题,建议使用版本5。这就是我所做的:

    npm i graphql-tools@5.0.0
    

    我重新启动了服务器并刷新了Playground page,瞧,我已经有了@include 指令和所有其他可用的指令。

    【讨论】:

      猜你喜欢
      • 2019-03-19
      • 2019-07-24
      • 2022-01-02
      • 2018-05-06
      • 2020-08-14
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多