Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting cannot determine GraphQL output error?

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

like image 729
Krzysztof Kaczyński Avatar asked Nov 09 '19 14:11

Krzysztof Kaczyński


1 Answers

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 {
  ...
}
like image 65
Daniel Rearden Avatar answered Sep 29 '22 23:09

Daniel Rearden