【问题标题】:Apollo GraphQL Error: Query root type must be providedApollo GraphQL 错误:必须提供查询根类型
【发布时间】:2020-04-23 19:17:36
【问题描述】:

我有一个 Angular/Apollo GraphQL 实现生成基于 GraphQl 端点的打字稿代码,该端点正在呈现模式。我可以通过 Postman 查询端点并返回结果。但是,当我通过 npm 运行“graphql-codegen --config codegen.yml”时,我收到此错误:

“错误:必须提供查询根类型”

服务器端是使用 GraphQL ASPNetCore 的 .Net Core 实现。我定义了 4 个不同的查询,每个查询都通过 graphiql 工作。

关于为什么查询根类型现在返回为 null 的任何想法?

【问题讨论】:

  • 如果您正在运行的graphql-codegen 命令有问题,您应该在问题中包含codegen.yml 的内容并适当地标记您的问题。
  • graphql-codegen 命令与进程运行时相比没有变化。

标签: graphql apollo


【解决方案1】:

当您的架构拼接/定义不正确时会引发此错误。请检查检查您的根架构定义

https://www.advancedgraphql.com/content/schema-stitching

【讨论】:

  • 我已经从源代码中删除了除 1 个以外的所有查询,但仍然收到“必须提供查询根类型”。单个查询不应有任何拼接冲突。
  • 如果可能,请您分享您的代码。我可以看看吗
  • "当您的模式拼接/定义不正确时。" -- 或丢失/根本没有查询公开数据。没有查询的 GraphQL 模式有点像树倒在树林里的声音,周围没有人。除了 GraphQL 会抛出错误。 (-‸ლ)
【解决方案2】:

我最终恢复到以前版本的代码库并手动重新应用了我的修改,它现在可以工作了。我唯一能想到的就是我运行了 npm update,它把 apollo-angular 从 1.8.3 更新到了 1.10.0。

编辑这是我的代码:

codegen.yml(用于从 npm 命令生成代码):

overwrite: true
schema: "https://to_end_point/Prod/api/v1/GraphQL"
documents: "src/**/*.graphql"
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-apollo-angular"
  ./graphql.schema.json:
    plugins:
      - "introspection"

在恢复到以前版本的 Angular 代码然后重新应用我的代码修改后,GraphQl 代码生成再次工作。我能想到的唯一可能导致此问题的是我运行 npm update 时。下面是 package.json 之前/之后的截图:

【讨论】:

    【解决方案3】:

    我在使用 graphql-codegen 时遇到了同样的问题。

    我的 codegen.yml 是

    overwrite: true
    schema: "http://localhost:3001/graphql"
    documents: "src/app/graphql/*.graphql"
    generates:
      src/generated/graphql.ts:
        plugins:
          - typescript
          - typescript-operations
          - typescript-apollo-angular
    

    当我使用插件typescript-apollo-angular时出现问题。

    我使用Nodejs 和graphql 作为后端。

    重命名类型后问题得到解决 RootQuery -> 查询 和 RootMutation -> 变异 在后端架构中。

    之前

    
    type RootQuery {
      _empty: String  
    }
    
    type RootMutation {
      _empty: String
    }
    
    schema {
        query: RootQuery
        mutation: RootMutation
    }
    

    之后

    type Query {
      _empty: String  
    }
    
    type Mutation {
      _empty: String
    }
    
    schema {
        query: Query
        mutation: Mutation
    }
    
    

    【讨论】:

      【解决方案4】:

      GraphQL 必须至少有一个 @Query() 才能被视为有效。因此,也许只需要向您的解析器代码添加任何查询就会有所帮助。 例如:

      export class FooResolver {
      
        @Query(() => String)
        sayHello(): string {
          return 'Hello World!';
        }
      }
      

      【讨论】:

      • 嗨@Dung Le,您可以在将来回答问题之前询问示例代码,但是我很高兴您有兴趣为社区做出贡献。欢迎!!
      猜你喜欢
      • 2019-06-02
      • 2019-06-09
      • 2021-01-14
      • 2019-06-16
      • 2021-12-15
      • 2018-12-15
      • 2018-01-13
      • 2021-06-16
      • 2018-04-01
      相关资源
      最近更新 更多