【问题标题】:NestJS - Expected undefined to be a GraphQL schemaNestJS - 预期未定义为 GraphQL 模式
【发布时间】:2021-12-15 02:34:31
【问题描述】:

我正在尝试使用 NestJS 8 设置一个非常小的 GraphQL API。我从文档中安装了所有必需的 redepndencies,但是当我启动服务器时,我收到了这个错误:

[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)

我不明白这个错误,因为我只是按照文档...

// 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;
}

有人能指出我的项目有什么问题吗?

【问题讨论】:

    标签: typescript graphql nestjs


    【解决方案1】:

    我收到了同样的错误。 经过一步步调试,答案是@nestjs/graphql@9.1.1GraphQL@16不兼容。

    具体来说,GraphQL@16gqaphql 函数(从 graphqlImpl 中调用)更改为仅支持不带架构的 args:

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

    要解决此问题,您需要将 graphql 版本降级到 15.x。

    【讨论】:

    • 降级到版本 15.7.2 后,一切都开始工作了。你拯救了我的一天!!!
    • 也用 15.8.0 测试过,谢谢。
    【解决方案2】:

    我从nestjs 的官方文档中了解到。这是版本问题。

    要避免这个问题,只需安装

    npm i @nestjs/graphql graphql@^15 apollo-server-express
    

    为了更好地理解 - 参考nestjs的文档

    Graphql with nestjs

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 2020-06-01
      • 2021-03-17
      • 2019-04-11
      • 2020-09-15
      • 2020-08-25
      • 2019-10-01
      • 2019-10-06
      相关资源
      最近更新 更多