为什么我无法确定GraphQL输出错误?

我正在尝试使用 nest.js GraphQL MongoDB 创建简单的应用程序。我拥有使用 TypeORM TypeGraphql 生成我的架构并与本地主机数据库建立连接的功能,但是我无法使用nest start运行我的服务器,因为我正在获取此信息错误:

  

UnhandledPromiseRejectionWarning:错误:无法确定getarticles的GraphQL输出类型

我不知道为什么会收到此错误。我的班级ArticleEntity没有任何非主要类型,因此应该没有任何问题。我试图从() => ID类的@Field()的{​​{1}}装饰者中删除_id,但没有帮助

ArticleResolver

ArticleEntity

ArticleService

@Resolver(() => ArticleEntity)
export class ArticlesResolver {
  constructor(
    private readonly articlesService: ArticlesService) {}

  @Query(() => String)
  async hello(): Promise<string> {
    return 'Hello world';
  }

  @Query(() => [ArticleEntity])
  async getarticles(): Promise<ArticleEntity[]> {
    return await this.articlesService.findAll();
  }

}

ArticleEntity

@Injectable()
export class ArticlesService {
  constructor(
    @InjectRepository(ArticleEntity)
    private readonly articleRepository: MongoRepository<ArticleEntity>,) {}

  async findAll(): Promise<ArticleEntity[]> {
    return await this.articleRepository.find();
  }
}

ArticleDTO

@Entity()
export class ArticleEntity {
  @Field(() => ID)
  @ObjectIdColumn()
  _id: string;

  @Field()
  @Column()
  title: string;

  @Field()
  @Column()
  description: string;
}

如果您需要其他任何评论

squirrel917878 回答:为什么我无法确定GraphQL输出错误?

ArticleEntity应该用@ObjectType装饰器装饰,如文档中的here所示。

@Entity()
@ObjectType
export class ArticleEntity {
  ...
}
,

我使用的是MongoDB,我的Query返回了架构而不是模型类。

@Query((returns) => UserSchema)更改为@Query((returns) => User)可以解决此问题。

user.schema.ts

@ObjectType()
@Schema({ versionKey: `version` })
export class User {
    @Field()
    _id: string

    @Prop({ required: true })
    @Field()
    email: string

    @Prop({ required: true })
    password: string
}

export const UserSchema = SchemaFactory.createForClass(User)

user.resolver.ts

@Query((returns) => User)
async user(): Promise<UserDocument> {
    const newUser = new this.userModel({
        id: ``,email: `test@test.com`,password: `abcdefg`,})
    return await newUser.save()
}

,

就我而言,我使用的是@ObjectType装饰器,但是我是从type-graphql导入的。我是从@nestjs/graphql导入的,此问题已解决。

import { ObjectType } from '@nestjs/graphql';

有关GitHub上的相关讨论,请参见here

本文链接:https://www.f2er.com/3132580.html

大家都在问