【问题标题】:Why I am getting cannot determine GraphQL output error?为什么我无法确定 GraphQL 输出错误?
【发布时间】:2020-03-05 21:16:55
【问题描述】:

我正在尝试使用 Nest.jsGraphQLMongoDB 创建简单的应用程序。我想使用 TypeORMTypeGraphql 来生成我的架构并与 localhost 数据库建立连接,但是我无法使用nest start 运行我的服务器,因为我收到了这个错误:

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

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

ArticleResolver

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

}

文章服务

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

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

ArticleEntity

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

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

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

ArticleDTO

@InputType()
export class CreateArticleDTO {
  @Field()
  readonly title: string;

  @Field()
  readonly description: string;
}

如果您需要任何其他评论

【问题讨论】:

    标签: node.js typescript graphql nestjs


    【解决方案1】:

    ArticleEntity 应使用@ObjectType 装饰器进行装饰,如文档https://typegraphql.com/docs/types-and-fields.html 中所示。

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

    【讨论】:

    • 是的,我忘记了这个装饰器,这么小的东西,这么大的错误。谢谢老哥
    【解决方案2】:

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

    @Query((returns) =&gt; UserSchema) 更改为@Query((returns) =&gt; 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()
    }
    
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2019-11-29
        • 2020-07-07
        • 2021-02-20
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 2021-04-28
        • 2022-01-14
        相关资源
        最近更新 更多