Graphql: You need to provide explicit type for AuthorsResolver#getPosts !

Created on 29 Apr 2019  路  4Comments  路  Source: nestjs/graphql

I'm submitting a...


import { Resolver, ResolveProperty, Parent, Args, Query, Mutation } from '@nestjs/graphql';
import { Author } from './models/author';
import { AuthorsService } from './authors.service';
import { PostsService } from './../posts/posts.service';
import { Int } from 'type-graphql';
import { Post } from '../posts/models/post';

@Resolver(of => Author)
export class AuthorsResolver {
  constructor(
    private readonly authorsService: AuthorsService,
    private readonly postsService: PostsService
  ) {}

  @Query(returns => Author, { name: 'author' })
  async getAuthor(@Args({ name: 'id', type: () => Int }) id: number) {
    return await this.authorsService.findOneById(id);
  }

  @Mutation(returns => Post)
  async upvotePost(@Args({ name: 'postId', type: () => Int }) postId: number) {
    return await this.postsService.upvoteById({id: postId})
  }

  @ResolveProperty('posts')
  async getPosts(@Parent()  author): Promise {
    const { id } = author;
    return await this.postsService.findAll({ authorId: id });
  }
}

Current behavior


the application starts error like this


(node:13584) UnhandledPromiseRejectionWarning: Error: You need to provide explicit type for AuthorsResolver#getPosts !
    at definitions.forEach.def (E:\intrestspace\nest-study\node_modules\type-graphql\dist\metadata\metadata-storage.js:135:31)
    at Array.forEach ()
    at MetadataStorage.buildFieldResolverMetadata (E:\intrestspace\nest-study\node_modules\type-graphql\dist\metadata\metadata-storage.js:122:21)
    at MetadataStorage.build (E:\intrestspace\nest-study\node_modules\type-graphql\dist\metadata\metadata-storage.js:77:14)
    at Function.generateFromMetadataSync (E:\intrestspace\nest-study\node_modules\type-graphql\dist\schema\schema-generator.js:29:51)
    at Function. (E:\intrestspace\nest-study\node_modules\type-graphql\dist\schema\schema-generator.js:16:33)
    at Generator.next ()
    at E:\intrestspace\nest-study\node_modules\tslib\tslib.js:107:75
    at new Promise ()
    at Object.__awaiter (E:\intrestspace\nest-study\node_modules\tslib\tslib.js:103:16)
(node:13584) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:13584) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I fixed like this. alos error


@ResolveProperty('posts')
  async getPosts(@Args({name: 'author', type: () => Author}) @Parent()  author): Promise {
    const { id } = author;
    return await this.postsService.findAll({ authorId: id });
  }

Expected behavior

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: 6.0.0


For Tooling issues:
- Node version: 10.15.3
- Platform:  windows

Others:

Most helpful comment

Change:

@ResolveProperty('posts')

to:

@ResolveProperty('posts', () => [Post])

All 4 comments

same issue

Change:

@ResolveProperty('posts')

to:

@ResolveProperty('posts', () => [Post])

I have the same issue with a resolver like

Resolver

import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { Demand } from './demand';
import { DemandService } from './demand.service';
import { ObjectID } from 'typeorm';

@Resolver(Demand)
export class DemandResolver {
  constructor(private readonly demandService: DemandService) { }

  @Query(() => Demand)
  async demand(@Args('id') id: ObjectID) {
    return await this.demandService.findById(id);
  }

  @Query(() => [Demand])
  async demands() {
    return await this.demandService.findAll();
  }

  @Mutation(() => Demand)
  async demandCreate(@Args('input') input: Demand) {
    return await this.demandService.create(input);
  }

  @Mutation(() => Demand)
  async demandUpdate(
    @Args('id') id: ObjectID,
    @Args('input') input: Demand,
  ) {
    return await this.demandService.update(id, input);
  }

  @Mutation(() => Demand)
  async demandDelete(@Args('id') id: ObjectID) {
    return await this.demandService.delete(id);
  }
}

Entity

import {Field, InputType, ObjectType} from 'type-graphql';
import {Column, Entity, ObjectID, ObjectIdColumn} from 'typeorm';

@Entity()
@ObjectType()
@InputType('DemandInput')
export class Demand {
  @Field(type => ObjectID, { nullable: true })
  @ObjectIdColumn()
  id?: ObjectID;

  @Field({ nullable: true })
  @Column({ nullable: true })
  name?: string;
}

Current Behaviour

(node:22415) UnhandledPromiseRejectionWarning: Error: You need to provide explicit type for DemandResolver#demand parameter #0 !
    at Object.findType (/home/olivier/www/other/cyh/graphql-server/node_modules/type-graphql/dist/helpers/findType.js:17:15)
    at Object.getParamInfo (/home/olivier/www/other/cyh/graphql-server/node_modules/type-graphql/dist/helpers/params.js:9:49)
    at /home/olivier/www/other/cyh/graphql-server/node_modules/type-graphql/dist/decorators/Arg.js:9:159
    at /home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/decorators/args.decorator.js:34:113
    at /home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/storages/lazy-metadata.storage.js:11:36
    at Array.forEach (<anonymous>)
    at LazyMetadataStorageHost.load (/home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/storages/lazy-metadata.storage.js:11:22)
    at GraphQLSchemaBuilder.<anonymous> (/home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/graphql-schema-builder.js:31:57)
    at Generator.next (<anonymous>)
    at /home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/graphql-schema-builder.js:17:71
(node:22415) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:22415) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

But I don't understand why

I have the same issue with a resolver like

Resolver

import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { Demand } from './demand';
import { DemandService } from './demand.service';
import { ObjectID } from 'typeorm';

@Resolver(Demand)
export class DemandResolver {
  constructor(private readonly demandService: DemandService) { }

  @Query(() => Demand)
  async demand(@Args('id') id: ObjectID) {
    return await this.demandService.findById(id);
  }

  @Query(() => [Demand])
  async demands() {
    return await this.demandService.findAll();
  }

  @Mutation(() => Demand)
  async demandCreate(@Args('input') input: Demand) {
    return await this.demandService.create(input);
  }

  @Mutation(() => Demand)
  async demandUpdate(
    @Args('id') id: ObjectID,
    @Args('input') input: Demand,
  ) {
    return await this.demandService.update(id, input);
  }

  @Mutation(() => Demand)
  async demandDelete(@Args('id') id: ObjectID) {
    return await this.demandService.delete(id);
  }
}

Entity

import {Field, InputType, ObjectType} from 'type-graphql';
import {Column, Entity, ObjectID, ObjectIdColumn} from 'typeorm';

@Entity()
@ObjectType()
@InputType('DemandInput')
export class Demand {
  @Field(type => ObjectID, { nullable: true })
  @ObjectIdColumn()
  id?: ObjectID;

  @Field({ nullable: true })
  @Column({ nullable: true })
  name?: string;
}

Current Behaviour

(node:22415) UnhandledPromiseRejectionWarning: Error: You need to provide explicit type for DemandResolver#demand parameter #0 !
    at Object.findType (/home/olivier/www/other/cyh/graphql-server/node_modules/type-graphql/dist/helpers/findType.js:17:15)
    at Object.getParamInfo (/home/olivier/www/other/cyh/graphql-server/node_modules/type-graphql/dist/helpers/params.js:9:49)
    at /home/olivier/www/other/cyh/graphql-server/node_modules/type-graphql/dist/decorators/Arg.js:9:159
    at /home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/decorators/args.decorator.js:34:113
    at /home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/storages/lazy-metadata.storage.js:11:36
    at Array.forEach (<anonymous>)
    at LazyMetadataStorageHost.load (/home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/storages/lazy-metadata.storage.js:11:22)
    at GraphQLSchemaBuilder.<anonymous> (/home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/graphql-schema-builder.js:31:57)
    at Generator.next (<anonymous>)
    at /home/olivier/www/other/cyh/graphql-server/node_modules/@nestjs/graphql/dist/graphql-schema-builder.js:17:71
(node:22415) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:22415) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

But I don't understand why

Same issue

Was this page helpful?
0 / 5 - 0 ratings