Does NestJS execute resolvers of ObjectTypes when they are used as a fields of another ObjectType

194 Views Asked by At

Consider an ObjectType, as such:

@ObjectType()
@KeyFields('id') // wrapper to create federation directive
export class MyInterestingThing {
  @Field(type => ID)
  id: string;
  
  @Field()
  someField: string;

  @Field(type => SomeDetails)
  someDetails: SomeDetails;
}

which has a nested ObjectType as follows:

@ObjectType()
export class SomeDetails {
  @Field()
  name: string;

  @Field(type => ID)
  id: string
}

The parent object type has a resolver:

@Resolver(of => MyInterestingThing)
export class MyInterestingThingResolver {
  constructor(private readonly loader: MyInterestingThingLoader) {}

  @ResolveField(type => SomeDetails)
  async someDetails(@Parent() parent) {
    return { ...parent }
  }
  
  @ResolveReference()
  async resolveReference(ref: { __typename: string, id: string }) {
    return this.loader.byId.load(id);
  }
}

and the child type has a corresponding resolver, as well:

@Resolver(of => SomeDetails)
export class SomeDetailsResolver {
  constructor(private readonly detailLoader: SomeDetailLoader) {}
  
  @ResolveField()
  async name(@Parent() { someDetailId }) {
    const ref = await this.detailLoader.byId.load(someDetailId);
    
    return ref.name;
  }

  @ResolveField()
  async id(@Parent() { someDetailId }) {
    const ref = await this.detailLoader.byId.load(someDetailId);

    return ref.id;
  }
}

When I query for the following:

{
  myInterestingThing {
    someField
    someDetails {
      name
      id
    }
  }
}

My parent resolver executes, but the child resolver never does. Shouldn't Nest be passing execution to the SomeDetailsResolver in order to resolve that ObjectType? Is there something special that needs to get done to get this behavior to work? Or is my expectation of how this works incorrect?

I'd expect the resolvers for each ObjectType to execute so that we can keep concerns related to each object type encapsulated in their respective resolver, loader and service.

0

There are 0 best solutions below