Type-graphql: Field resolver is called without providing resolver class to `resolvers` property in `buildSchema`

Created on 20 Mar 2020  路  7Comments  路  Source: MichalLytek/type-graphql

I have this LaunchResolver defined like this:

@Resolver(() => Launch)
class LaunchResolver implements ResolverInterface<Launch> {
    // (...)
}

The Launch type is something like this:

@ObjectType()
class Launch {
    // (other fields)

    @Field(() => Mission)
    mission!: Mission;
}

And then I added a MissionResolver with a @FieldResolver:

enum PatchSize {
    LARGE,
    SMALL,
}

registerEnumType(PatchSize, {
    name: 'PatchSize',
});

@Resolver(() => Mission)
class MissionResolver implements ResolverInterface<Mission> {
    @FieldResolver()
    patchUrl(
        @Root() mission: Mission,
        @Arg('size', () => PatchSize, { nullable: true }) size = PatchSize.SMALL
    ): string | undefined {
        return size === PatchSize.SMALL ? mission.smallPatchUrl : mission.largePatchUrl;
    }
}

And, for reference (in case it's relevant), this is the Mission type:

@ObjectType()
class Mission {
    @Field({ nullable: true })
    name?: string;

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

    smallPatchUrl?: string;

    largePatchUrl?: string;
}

Everything was working perfectly with the code above and it was when I realized that my schema was being defined like this:

const schema = buildSchemaSync({
    container: ({ context }: ResolverData<GraphQLContext>) => {
        return context.container;
    },
    resolvers: [LaunchResolver],
});

As you can see, MissionResolver is missing from the resolvers array but everything still works perfectly and patchUrl is successfully resolved with the @FieldResolver. I tested adding MissionResolver to the resolvers array and it worked just the same.

My question is two-fold:

  1. Why is my code working without adding MissionResolver to the resolvers array?
  2. What's the recommended best practice for this? Should I always add all resolvers or just the "main" one?
Bug Community Solved

All 7 comments

Why is my code working without adding MissionResolver to the resolvers array?

See #234 馃憖

What's the recommended best practice for this?

Update to type-graphql@beta 馃槈

Why is my code working without adding MissionResolver to the resolvers array?

See #234 馃憖

Based on my basic understanding of what I read, it feels like a somewhat different issue but maybe related, couldn't tell.

What's the recommended best practice for this?

Update to type-graphql@beta 馃槈

Updated to 0.18.0-beta.12 but I didn't notice any difference. Everything still worked as I expected without adding MissionResolver to the resolvers array so my quesion kinda persists... Is it supposed to?

Oh, indeed, field resolvers are not filtered by resolvers array, like queries or mutations, but should be 馃槺

Everything still worked as I expected without adding MissionResolver to the resolvers array

Because metadata storage is global to collect metadata from each decorator, so this is the leak because of no proper filtering 馃槈

Fixed via 86d1fea 馃敀

Awesome, thanks 馃憤

Now eagerly waiting for the next beta release 鈽猴笍

@rfgamaral
Released as v0.18.0-beta.14 馃挭

I just tested this. It worked great, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glentakahashi picture glentakahashi  路  3Comments

MichalLytek picture MichalLytek  路  3Comments

tafelito picture tafelito  路  3Comments

Janushan picture Janushan  路  3Comments

MichalLytek picture MichalLytek  路  3Comments