Apollo GraphQL错误:必须提供查询根类型

huangapple go评论78阅读模式
英文:

Apollo GraphQL Error: Query root type must be provided

问题

I have an Angular/Apollo GraphQL implementation generating typescript code based on GraphQl endpoint which is surfacing a schema. I can hit the endpoint via Postman with a query and results are returned. However, when I run "graphql-codegen --config codegen.yml" via npm I get this error:

"Error: Query root type must be provided"

The server side is .Net Core implementation using GraphQL ASPNetCore. I have 4 different queries defined and each one works via graphiql.

Any ideas on why query root type is now being returned as null?

英文:

I have an Angular/Apollo GraphQL implementation generating typescript code based on GraphQl endpoint which is surfacing a schema. I can hit the endpoint via Postman with a query and results are returned. However, when I run "graphql-codegen --config codegen.yml" via npm I get this error:

"Error: Query root type must be provided"

The server side is .Net Core implementation using GraphQL ASPNetCore. I have 4 different queries defined and each one works via graphiql.

Any ideas on why query root type is now being returned as null?

答案1

得分: 8

GraphQL必须至少有一个@Query()才被视为有效。所以只需在您的解析器代码中添加任何查询可能会有所帮助。

例如:

export class FooResolver {

  @Query(() => String)
  sayHello(): string {
    return 'Hello World!';
  }
}
英文:

GraphQL must have at least one @Query() to be considered valid. So maybe only need add any Query to your Resolver code will be helpful.
Ex:

export class FooResolver {

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

</details>



# 答案2
**得分**: 1

这个错误是在您的模式拼接/定义不正确时引发的。请检查您的根模式定义。

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

<details>
<summary>英文:</summary>

This error throws when your schema stiching/definitions are incorrect. Please check the check your root schema definitions


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

</details>



# 答案3
**得分**: 1

I was having the same issue while using `graphql-codegen`.

my codegen.yml is

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


The issue was coming when I used the plugin `typescript-apollo-angular`.

I'm using `Nodejs` with `graphql` as backend.

The issue got resolved when I renamed the type
RootQuery -> Query
and
RootMutation -> Mutation
in backend **schema**.

Before

type RootQuery {
_empty: String
}

type RootMutation {
_empty: String
}

schema {
query: RootQuery
mutation: RootMutation
}



After

```javascript
type Query {
  _empty: String  
}

type Mutation {
  _empty: String
}

schema {
    query: Query
    mutation: Mutation
}
英文:

I was having the same issue while using graphql-codegen.

my codegen.yml is

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

The issue was coming when I used the plugin typescript-apollo-angular.

I'm using Nodejs with graphql as backend.

The issue got resolved when I renamed the type
RootQuery -> Query
and
RootMutation -> Mutation
in backend schema.

Before


type RootQuery {
  _empty: String  
}

type RootMutation {
  _empty: String
}

schema {
    query: RootQuery
    mutation: RootMutation
}

After

type Query {
  _empty: String  
}

type Mutation {
  _empty: String
}

schema {
    query: Query
    mutation: Mutation
}

答案4

得分: 0

我最终还原到了代码库的先前版本,并手动重新应用了我的修改,现在它可以工作了。我唯一能想到的是我运行了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的前后截图:

Apollo GraphQL错误:必须提供查询根类型

英文:

I ended up reverting back to a previous version of the codebase and reapplied my modifications manually and it works now. The only thing I can think of is I ran npm update which updated apollo-angular from 1.8.3 to 1.10.0.

EDIT Here is my code:

codegen.yml (used to generate code from npm command):

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

After reverting back to a previous version of Angular code then re-applying my code modifications, GraphQl code generation worked again. The only thing I can think of which could have caused this issue was when I ran npm update. Below is a screenshot of before/after of package.json:

Apollo GraphQL错误:必须提供查询根类型

huangapple
  • 本文由 发表于 2020年1月6日 16:26:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定