Categories
discuss

NestJS – Expected undefined to be a GraphQL schema

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, …

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.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..