【问题标题】:NestJs: Make sure your class is decorated with an appropriate decoratorNestJs:确保你的班级用合适的装饰器装饰
【发布时间】:2021-05-19 08:36:17
【问题描述】:

我使用graphql-request 作为 GraphQL 客户端来查询无头 CMS 以获取内容、修改并返回到原始请求/查询。无头 cms 单独托管,仅供参考。

我有以下代码:

@Query(returns => BlogPost)
  async test() {
    const endpoint = 'https://contentxx.com/api/content/project-dev/graphql'
    const graphQLClient = new GraphQLClient(endpoint, {
      headers: {
        authorization: 'Bearer xxxxxxx',
      },
    })
    const query = gql`
      {
        findContentContent(id: "9f5dde89-7f9b-4b9c-8669-1f0425b2b55d") {
          id
          flatData {
            body
            slug
            subtitle
            title
          }
        }
      }`

    return await graphQLClient.request(query);
  }

BlogPost 是具有以下类型的模型:

import { Field, ObjectType } from '@nestjs/graphql';
import { BaseModel } from './base.model';
import FlatDateType from '../resolvers/blogPost/types/flatDatatype.type';

@ObjectType()
export class BlogPost extends BaseModel {
  @Field({ nullable: true })
  id!: string;

  @Field((type) => FlatDateType)
  flatData: FlatDateType;
}

FlatDateType有如下代码

export default class FlatDateType {
  body: string;
  slug: string;
  subtitle: string;
  title: string;
}

它抛出以下异常:

错误:无法确定“flatData”的 GraphQL 输出类型。制作 确保您的班级使用适当的装饰器进行装饰。

这里缺少什么?

【问题讨论】:

    标签: javascript node.js typescript graphql nestjs


    【解决方案1】:

    当没有关于 FlatDataType 的信息被传递给 graphql 解析器时,您的 graphql 服务器应该如何理解它的类型?您还需要将 graphql 装饰器添加到其中。 @ObjectType()@Field()

    【讨论】:

      【解决方案2】:

      FlatDataType 未定义为 @ObjectType(),因此 type-graphql(或 @nestjs/graphql)不能将其作为 GraphQL 中的输出。

      【讨论】:

        猜你喜欢
        • 2010-10-15
        • 2020-06-26
        • 2021-03-17
        • 2015-08-13
        • 1970-01-01
        • 2016-01-13
        • 2019-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多