最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NestJs: Make sure your class is decorated with an appropriate decorator - Stack Overflow

programmeradmin1浏览0评论

I am using graphql-request as a GraphQL client to query a headless CMS to fetch stuff, modify and return to the original request/query. headless cms is hosted separately fyi.

I have the following code :

@Query(returns => BlogPost)
  async test() {
    const endpoint = ''
    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 is a model having the types :

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

and FlatDateType has the following code

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

it throws the following exception :

Error: Cannot determine a GraphQL output type for the "flatData". Make sure your class is decorated with an appropriate decorator.

What is missing in here?

I am using graphql-request as a GraphQL client to query a headless CMS to fetch stuff, modify and return to the original request/query. headless cms is hosted separately fyi.

I have the following code :

@Query(returns => BlogPost)
  async test() {
    const endpoint = 'https://contentxx./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 is a model having the types :

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

and FlatDateType has the following code

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

it throws the following exception :

Error: Cannot determine a GraphQL output type for the "flatData". Make sure your class is decorated with an appropriate decorator.

What is missing in here?

Share Improve this question asked Feb 16, 2021 at 13:29 GammerGammer 5,62820 gold badges82 silver badges132 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

How is your graphql server supposed to understand the type of FlatDataType when there's no information about it being passed to the graphql parser? You need to add the graphql decorators to it as well. @ObjectType(), @Field(), etc.

FlatDataType is not defined as @ObjectType(), therefore type-graphql (or @nestjs/graphql) can't take it as an output in GraphQL.

发布评论

评论列表(0)

  1. 暂无评论