I am trying to setup a very small GraphQL API using NestJS 8. I installed all required redepndencies from the documentation, but when I start the server, I get this error:
[Nest] 22727 - 10/30/2021, 10:11:10 AM LOG [NestFactory] Starting Nest application... [Nest] 22727 - 10/30/2021, 10:11:10 AM LOG [InstanceLoader] AppModule dependencies initialized +43ms [Nest] 22727 - 10/30/2021, 10:11:10 AM LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms [Nest] 22727 - 10/30/2021, 10:11:10 AM LOG [InstanceLoader] ConfigHostModule dependencies initialized +7ms [Nest] 22727 - 10/30/2021, 10:11:10 AM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms [Nest] 22727 - 10/30/2021, 10:11:10 AM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms [Nest] 22727 - 10/30/2021, 10:11:11 AM LOG [InstanceLoader] GraphQLSchemaBuilderModule dependencies initialized +21ms [Nest] 22727 - 10/30/2021, 10:11:11 AM LOG [InstanceLoader] GraphQLModule dependencies initialized +1ms [Nest] 22727 - 10/30/2021, 10:11:11 AM LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +93ms [Nest] 22727 - 10/30/2021, 10:11:11 AM LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms [Nest] 22727 - 10/30/2021, 10:11:11 AM LOG [InstanceLoader] PostModule dependencies initialized +0ms /workspace/node_modules/graphql/type/schema.js:35 throw new Error( ^ Error: Expected undefined to be a GraphQL schema. at assertSchema (/workspace/node_modules/graphql/type/schema.js:35:11) at validateSchema (/workspace/node_modules/graphql/type/validate.js:34:28) at graphqlImpl (/workspace/node_modules/graphql/graphql.js:52:64) at /workspace/node_modules/graphql/graphql.js:21:43 at new Promise (<anonymous>) at graphql (/workspace/node_modules/graphql/graphql.js:21:10) at GraphQLSchemaFactory.create (/workspace/node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:48:60) at GraphQLSchemaBuilder.buildSchema (/workspace/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:62:52) at GraphQLSchemaBuilder.build (/workspace/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:24:31) at GraphQLFactory.mergeOptions (/workspace/node_modules/@nestjs/graphql/dist/graphql.factory.js:33:69)
I don’t understand this error, as I am just following the documentation…
// app.module.ts import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { GraphQLModule } from '@nestjs/graphql'; import { TypeOrmModule } from '@nestjs/typeorm'; import { GraphqlOptions } from './config/graphql.config'; import { typeOrmConfigAsync } from './config/typeorm.config'; import { PostModule } from './post/post.module'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true }), TypeOrmModule.forRootAsync(typeOrmConfigAsync), GraphQLModule.forRootAsync({ useClass: GraphqlOptions, }), PostModule, ], }) export class AppModule {}
// graphql.config.ts import { Injectable } from '@nestjs/common'; import { GqlModuleOptions, GqlOptionsFactory } from '@nestjs/graphql'; @Injectable() export class GraphqlOptions implements GqlOptionsFactory { createGqlOptions(): Promise<GqlModuleOptions> | GqlModuleOptions { return { autoSchemaFile: 'schema.gql', sortSchema: true, debug: true, installSubscriptionHandlers: true, context: ({ req }) => ({ req }), }; } }
// post.module.ts import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Post } from './post.entity'; import { PostResolver } from './post.resolver'; import { PostService } from './post.service'; @Module({ imports: [TypeOrmModule.forFeature([Post])], providers: [PostService, PostResolver], exports: [PostService], }) export class PostModule {}
// post.entity.ts import { Field, ID, ObjectType } from '@nestjs/graphql'; import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; @Entity('post') @ObjectType() export class Post { @Field(() => ID) @PrimaryGeneratedColumn('uuid') id: string; @Field() @Column({ nullable: false }) title: string; @Field() @Column({ nullable: false, unique: true }) slug: string; @Field() @Column({ nullable: false }) content: string; @Field() @Column({ type: 'timestamp' }) createdAt: Date; @Field() @Column({ type: 'timestamp', nullable: true }) updatedAt: Date; }
Does anyone can highlight what’s wrong with my project?
Answer
I was receiving the same errors.
After debugging step by step, the answer is that @nestjs/graphql@9.1.1
is not compatible with GraphQL@16
.
Specifically, GraphQL@16
changed the gqaphql
function, as called from within graphqlImpl
, to only support args without a schema:
function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) { var _arguments = arguments; /* eslint-enable no-redeclare */ // Always return a Promise for a consistent API. return new Promise(function (resolve) { return resolve( // Extract arguments from object args if provided. _arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({ schema: argsOrSchema, source: source, rootValue: rootValue, contextValue: contextValue, variableValues: variableValues, operationName: operationName, fieldResolver: fieldResolver, typeResolver: typeResolver })); }); }
To resolve, you will need to downgrade your graphql
version to 15.x.