7

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?

2
  • 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. Commented Jan 6, 2020 at 10:58
  • graphql-codegen command has not changed from when the process was working. Commented Jan 6, 2020 at 14:52

4 Answers 4

8

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!';
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Dung Le, you can ask for the examples code before answer a question in the future, however I'm happy that you are interested to contributed for the community. Welcome!!
1

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

3 Comments

I've removed all but 1 query from my source code and am still getting 'Query root type must be provided'. There should not be any stitching conflicts with a single query.
If possible can you please share your code. Can i have a look
"when your schema stiching/definitions are incorrect." -- or missing/there's no query exposing data at all. A GraphQL schema without a query is kind of like the sound of a tree falling in the woods with nobody around. Except GraphQL throws an error. (-‸ლ)
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

type Query {
  _empty: String  
}

type Mutation {
  _empty: String
}

schema {
    query: Query
    mutation: Mutation
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.