5
votes

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?

4
If it's an issue with the graphql-codegen command you're running, you should include the content of the codegen.yml in your question and tag your question appropriately. - Daniel Rearden
graphql-codegen command has not changed from when the process was working. - Tom Schreck

4 Answers

3
votes

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!';
  }
}
1
votes

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

1
votes

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

type Query {
  _empty: String  
}

type Mutation {
  _empty: String
}

schema {
    query: Query
    mutation: Mutation
}

0
votes

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: "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"

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:

enter image description here