gpt4 book ai didi

javascript - NestJS - 预期未定义为 GraphQL 模式

转载 作者:行者123 更新时间:2023-12-05 00:25:07 25 4
gpt4 key购买 nike

我正在尝试使用 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;
}
有谁可以突出我的项目有什么问题?

最佳答案

我收到了同样的错误。
经过一步步调试,答案是@nestjs/graphql@9.1.1GraphQL@16 不兼容.
具体来说,GraphQL@16更改了gqaphql函数,从 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。

关于javascript - NestJS - 预期未定义为 GraphQL 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69778679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com