AuntiSpam
AuntiSpam

Reputation: 31

NestJS: Problem with using ResolveProperty. Got " UnhandledPromiseRejectionWarning: TypeError:"

I have the parent model (camera) that contains the id of child (conveyor) as shown below:

@ObjectType('camera')

export class CameraModel {
  @Field(() => ID)
  _id: String;

  @Field()
  name: String;

  @Field(() => ConveyorModel)
  conveyor: ConveyorModel;
}

So in order to get detail of "conveyor" i need to use @ResolveProperty decorator in camera.resolver.ts (note: showing only related method)

import { CameraModel } from './models/camera.model';
import { ConveyorService } from "../conveyor/conveyor.service";
import { ConveyorModel } from "../conveyor/models/conveyor.model";

@Injectable()
export class CameraResolver {
  constructor(    
    private conveyorService: ConveyorService, 
    private cameraService: CameraService,
    ) {}

  @ResolveProperty('conveyor', type => ConveyorModel)  
  async getConveryor(@Parent() CameraModel) {
    const { cid } = CameraModel;
    return this.conveyorService.findOne(cid);     
  }
}

I got below error after run the server. Mostly from the graphql schema generator.

(node:30636) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getObjectType' of undefined

...

(node:30636) 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:30636) [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.

However if i comment out the @ResolveProperty block, everything went fine. But i can't just get detail of the conveyor (only get ID).

What am I missing here?

Upvotes: 1

Views: 1345

Answers (1)

AuntiSpam
AuntiSpam

Reputation: 31

Based on my finding related issues at https://github.com/nestjs/graphql/issues/158

It has been fixed.

Don't use string base decorator of @Resolver('YourModel'). Just use @Resolver(of => YourModel);

The document of NestJS in this part is really insufficient.

Upvotes: 2

Related Questions