GraphQLError: Query root type must be provided

GraphQLError: Query root type must be provided


37

I’m using NestJS, TypeORM and GraphQL for my backend API. I’m getting the following error:

GraphQLError [Object]: Query root type must be provided.
      at SchemaValidationContext.reportError (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/type/validate.js:88:19)
      at validateRootTypes (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/type/validate.js:107:13)
      at validateSchema (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/type/validate.js:52:3)
      at graphqlImpl (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/graphql.js:79:62)
      at /home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/graphql.js:28:59
      at new Promise (<anonymous>)
      at Object.graphql (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/graphql.js:26:10)
      at GraphQLSchemaFactory.<anonymous> (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:49:52)
      at Generator.next (<anonymous>)
      at /home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/tslib/tslib.js:114:75

This is what my file structure and code looks like:

GraphQLError: Query root type must be provided

Can someone please help me. My repo: https://github.com/wise-introvert/nestjs-graphql-api.git

6 Answers
6


77

All servers running with GraphQL must have at least one @Query() to be considered a valid GraphQL server. Without it, the apollo-server package will throw an exception and the server will fail to start. This can be as simple as

@Resolver()
export class FooResolver {

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

1

  • 2

    I've added a dummy function under @Query decorator in my resolver but it's still throwing the same error.

    – Fardeen Panjwani

    Sep 28, 2020 at 16:29


52

Also ensure the Resolver is added in the module providers

@Module({
  imports: [
    GraphQLModule.forRoot({
      installSubscriptionHandlers: true,
      autoSchemaFile: true,
    }),
  ],
  controllers: [],
  providers: [FooResolver], //< This
})
export class FooModule {}

2

  • 2

    Thanks! This was the issue for me. The accepted answer did not solve the bug for me.

    – Royal

    Mar 29, 2021 at 7:42

  • What was missing for me was autoSchemaFile: true in the options object.

    – Fernando Basso

    Jun 23 at 11:45


4

This answer is not a direct answer to the question.

But if you are facing the described problem and the answers don’t help, recheck if you are using the correct imports:

// Correct
import { Resolver, Query } from '@nestjs/graphql';

// Incorrect in NestJS
import { Resolver, Query } from 'type-graphql';


2

In my case I had nfts.module.ts which is registered in app.module.ts but it was not configured properly. I had to pass providers

@Module({ providers: [NftsResolver, NftsService] })
export class NftsModule {}


1

In my case, I forgot to add my resolver file on the providers array on my Module.

After running it again, everything went fine and schema.gql file was created at the same time.

Just make sure to add both Service and Resolver to your providers. 😀


0

I hit this when working through the Nest.js GraphQL tutorial as ‘Schema first’.

As specified in the Schema first section of quick start, I still needed to set the typePaths and definitions options:

@Module({
  imports: [
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      typePaths: ['./**/*.graphql'],
      definitions: {
        path: join(process.cwd(), 'src/graphql.ts'),
        outputAs: 'class',
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})

This should then find any *.graphql file that you have created.



Leave a Reply

Your email address will not be published. Required fields are marked *