Search code examples
node.jstypescriptgraphqlnestjs

Why I am getting the error "cannot determine GraphQL output type"?


I am trying to create simple appliaction with Nest.js, GraphQL and MongoDB. I wnated to use TypeORM and TypeGraphql to generate my schema and make a connection with localhost databasebut but i can not run my server with nest start becouse I am getting this error:

UnhandledPromiseRejectionWarning: Error: Cannot determine GraphQL output type for getArticles

I have no idea why i am getting this error. My class ArticleEntity does't has any not primary types, so there should not be any problem. I tried to remove () => ID from @Field() decorator of filed _id of ArticleEntity class but it didn't helped

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

}

ArticleService

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

If you need anything else comment


Solution

  • ArticleEntity should be decorated with the @ObjectType decorator as shown in the docs https://typegraphql.com/docs/types-and-fields.html.

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