graphql: ResolveProperty('__resolveType') not working with type-graphql

The last few days I migrated our project to use type-graphql with nest. I managed to migrate everything, but I am still stuck on one thing. Providing a custom __typename doesn’t seem to work.

Here is my resolver:

@Resolver(ServiceProvider)
export class ServiceProviderResolvers {
    constructor(private readonly searchService: SearchService) {}

    @Query(() => [ServiceProvider])
    all(): Promise<ServiceProvider[]> {
        return this.searchService.all();
    }

    @ResolveProperty('__resolveType')
    resolveType(obj: ServiceProvider): string {
         return obj.type;
    }

    @ResolveProperty()
    test(obj: ServiceProvider): string {
         return obj.type;
    }
}

But the resolveType doesn’t seem to be called, where test method is called if I query for test field.

I also tried to use some interface like suggested 19majkel94 (from type-graphql) but still doesn’t work: https://github.com/19majkel94/type-graphql/issues/181#issuecomment-475166589

Is there any way to customize __typename with nest using type-graphql?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 26 (8 by maintainers)

Most upvoted comments

@kamilmysliwiec While creating a test project, I finally figured it out.

In my tsconfig.json I had target: es5. This works fine, until you start using @ResolveProperty. The resolution for this is switching target to target: es6.

Also you are not passing propertyName to the FieldResolver from type-graphql.

We don’t have to - Nest is using type-graphql only to generate GraphQL definitions.

Actually, we have to. According to TypeGraphQL docs:

Note that if a field name of a field resolver doesn’t exist in resolver object type, it will create in schema a field with this name. This feature is useful when the field is purely calculable (eg. averageRating from ratings array) and you don’t want to pollute the class signature.

So @apiel code will produce a Recipe type with id, title, … and resolveType: String!. Changing the name to __resolveType I guess will result with schema generation error as graphql-js doesn’t allow to create fields with name starting with an underscore.

There is a pending TypeGraphQL issue about that, as by default it uses instance of checks to resolve the type that might be not convenient when the 3rd party library like Typegoose doesn’t use class instances. https://github.com/19majkel94/type-graphql/issues/181

@apiel __typename means type name - __typename of type Recipe = Recipe and you can’t change that.

You can only provide a custom __resolveType function that will return a hint for graphql-runtime about the underlying type of the runtime data you are returning, like generic interface Person and specific type Student.

It may read __typename property value of data object (default behavior), check object prototype/instance of or use the type property like in your example (return obj.type).