Krzysztof Kaczyński
Krzysztof Kaczyński

Reputation: 5071

Why I am getting NoExplicitTypeError?

I am createing Nest.js application and I decided to create my project with GrapQL. I read some documentation and I decided to go code first approach(GraphQL schema should automagically render itself). For this approach it was needed to install type-graphql which is really cool, but after I created some simple examples with only primary types I decided to do something more challanging.

In my appliaction I have BlogEntity(from those Entities type-graphql is generating GraphQL schema) which is using ArticleEntity becouse it is needed to add typing to posts, projects and tutorials. I created my resolver and defined @ResolveProperty for posts, projects and tutorials. When I am trying to run nest start command i am getting this error:

throw new errors_1.NoExplicitTypeError(prototype.constructor.name, propertyKey, parameterIndex);

NoExplicitTypeError: You need to provide explicit type for BlogEntity#posts !

Can someone explain me why I am getting this error and what should I do to fix it?

BlogEntity

import { Entity, Column, ObjectIdColumn } from 'typeorm';
import { Field, ID, ObjectType } from 'type-graphql';
import { ArticleEntity } from '../../articles/models/article.entity';

@Entity()
@ObjectType()
export class BlogEntity {
  @Field(() => ID)
  @ObjectIdColumn()
  _id: string;

  @Field()
  @Column()
  posts: ArticleEntity[];

  @Field()
  @Column()
  projects: ArticleEntity[];

  @Field()
  @Column()
  tutorials: ArticleEntity[];
}

BlogResolver

@Resolver(() => BlogEntity)
export class BlogResolver {
  constructor(
    private readonly blogService: BlogService,
    private readonly articlesService: ArticlesService) {}

  @Query(() => String)
  async helloBlog(): Promise<string> {
    return 'Hello blog';
  }

  @ResolveProperty('posts', () => ArticleEntity)
  async getPosts(): Promise<ArticleEntity[]> {
    return await this.articlesService.findAll();
  }

  @ResolveProperty('projects', () => ArticleEntity)
  async getProjects(): Promise<ArticleEntity[]> {
    return await this.articlesService.findAll();
  }

  @ResolveProperty('tutorials', () => ArticleEntity)
  async getTutorials(): Promise<ArticleEntity[]> {
    return await this.articlesService.findAll();
  }

}

If you need anything else ping me in the comment section

Upvotes: 1

Views: 4868

Answers (1)

Krzysztof Kaczyński
Krzysztof Kaczyński

Reputation: 5071

So problem was not in @ResolverProperty and my resolver. Problem of this error was in deffinition of BlogEntity fields: posts, projects and tutorials. You can ask why? So for every type which is not primary (expect number there you also have to define what it will be Float or maybe Integer) and also for every field which is an array even array of primary types like string you also have to define a type of returnig values. How to do it, you just need to add returnTpyFunction to @Field() decorator.

Example

@Entity()
@ObjectType()
export class BlogEntity {
  @Field(() => ID)
  @ObjectIdColumn()
  _id: string;

  @Field(() => [ArticleEntity])
  @Column()
  posts: ArticleEntity[];

  @Field(() => [ArticleEntity])
  @Column()
  projects: ArticleEntity[];

  @Field(() => [ArticleEntity])
  @Column()
  tutorials: ArticleEntity[];
}

Upvotes: 4

Related Questions