【问题标题】:"Can only create NonNull of a Nullable GraphQLType but got: ID"“只能创建 Nullable GraphQLType 的 NonNull 但得到:ID”
【发布时间】:2020-02-04 06:11:26
【问题描述】:

运行apollo client:codegen 时出现以下错误:

  ✔ Loading Apollo Project
  ✖ Generating query files with 'typescript' target
    → Can only create NonNull of a Nullable GraphQLType but got: ID.
Error: Can only create NonNull of a Nullable GraphQLType but got: ID.
    at invariant (~/Code/humu/node_modules/graphql/jsutils/invariant.js:18:11)
    at new GraphQLNonNull (~/Code/humu/node_modules/graphql/type/definition.js:779:84)
    at Object.typeFromASTImpl (~/Code/humu/node_modules/graphql/utilities/typeFromAST.js:39:25)
    at variables.map.node (~/Code/humu/node_modules/apollo-codegen-core/lib/compiler/index.js:73:36)
    at Array.map (<anonymous>)
    at Compiler.compileOperation (~/Code/humu/node_modules/apollo-codegen-core/lib/compiler/index.js:71:75)
    at Object.compileToIR (~/Code/humu/node_modules/apollo-codegen-core/lib/compiler/index.js:15:44)
    at Object.generate [as default] (~/Code/humu/node_modules/apollo/lib/generate.js:78:36)
    at write (~/Code/humu/node_modules/apollo/lib/commands/client/codegen.js:67:54)
    at Task.task (~/Code/humu/node_modules/apollo/lib/commands/client/codegen.js:86:46)

为什么会这样?该错误听起来好像架构有两个非空运算符(例如:ID!!)或类似的,但在我的架构文件中我没有。

【问题讨论】:

    标签: graphql-js


    【解决方案1】:

    可能是您将两个不同版本的graphql-js 加载到内存中。在内部,包执行instanceof 检查以确定某些东西是否是有效的 GraphQL 类型,如果对象从一个副本传递到另一个副本,则检查将错误地返回 false。

    我们的团队遇到了这个问题,并通过yarn resolution 解决了这个问题:

    // in package.json
    "resolutions": {
      "graphql": "14.3.1"
    }
    

    (另外:事实证明,运行 yarn add 时不遵守分辨率的 yarn 1.x has a bug。仅运行 yarn 可以解决此问题。)

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 2016-12-28
      • 2017-09-06
      • 1970-01-01
      • 2012-11-09
      • 2018-10-06
      • 2014-10-11
      • 2014-03-10
      • 1970-01-01
      相关资源
      最近更新 更多