Skip to content
Advertisement

NestJs: Make sure your class is decorated with an appropriate decorator

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.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 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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement