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)
See https://typegraphql.ml/docs/interfaces.html
@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 totarget: es6.Actually, we have to. According to TypeGraphQL docs:
So @apiel code will produce a
Recipetype with id, title, … andresolveType: String!. Changing the name to__resolveTypeI guess will result with schema generation error asgraphql-jsdoesn’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 ofchecks 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
__typenamemeans type name -__typenameoftype Recipe=Recipeand you can’t change that.You can only provide a custom
__resolveTypefunction that will return a hint for graphql-runtime about the underlyingtypeof the runtime data you are returning, like genericinterface Personand specifictype Student.It may read
__typenameproperty value of data object (default behavior), check object prototype/instance ofor use thetypeproperty like in your example (return obj.type).