Home > Net >  NestJS - Expected undefined to be a GraphQL schema
NestJS - Expected undefined to be a GraphQL schema

Time:10-31

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?

CodePudding user response:

I got same errors. After debug step one by one ,the answer is @nestjs/[email protected] not compatible with GraphQL@16. GraphQL@16 changed the function:gqaphql when call graphqlImpl inside only support args without scheme. Pls downgrade your graphql version to 15.

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
    }));
  });
}
  • Related