type-graphql
type-graphql copied to clipboard
Resolver inheritance: `declarations: true` example
Describe the issue
The inheritance examples don't work with declarations: true
in tsconfig
(as actually noted in a hint). But there is no concrete example to make it work and I couldn't find a solution to this for many hours.
This is a proposed example for this case.
Are you able to make a PR that fix this? Yes, if wanted.
Additional context Instead of this hint:
Be aware that with some tsconfig.json settings (like declarations: true) we might receive a [ts] Return type of exported function has or is using private name 'BaseResolver' error - in this case we might need to use any as the return type or create a separate class/interface describing the class methods and properties.
we could (also?) recommend the following code which also works with declarations: true
:
export interface IBaseResolver<T> {
getAll(first: number): Promise<T[]>
}
export function createBaseResolver<T extends ClassType>(
suffix: string, objectTypeCls: T
): new() => IBaseResolver<T> {
@Resolver({ isAbstract: true })
class BaseResolver implements IBaseResolver<T> {
protected items: T[] = [];
@Query(type => [objectTypeCls], { name: `getAll${suffix}` })
async getAll(@Arg("first", type => Int) first: number): Promise<T[]> {
return this.items.slice(0, first);
}
}
return BaseResolver;
}
Note that making the BaseResolver
class not abstract
is required here. This should work well and is what I am using, since I need composite: true
and therefore declarations: true
.
Please use a proper issue template:
This should work well and is what I am using
Of course it can work and I was doing it in examples for a long period. But this force you to redeclare the whole shape of the class as an interface which is a huge duplication and I can't recommend such workaround in docs that people doesn't read because it's too big and complex.
Please use a proper issue template
I actually started it with that but interpreted that template as only a guide for what to include instead of seeing it as a required format, sry. I updated the issue to follow the template.
[...] I can't recommend such workaround in docs that people doesn't read because it's too big and complex.
Fair enough. Maybe linking to this issue is a possibility? Or a hidden page of the docs which explains exactly just that one case? Maybe like this:
Be aware that with some tsconfig.json settings (like declarations: true) we might receive a [ts] Return type of exported function has or is using private name 'BaseResolver' error - in this case we might need to use any as the return type or create a separate class/interface describing the class methods and properties. If you need help using an interface for this, see here.