type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

Resolver inheritance: `declarations: true` example

Open fw623 opened this issue 4 years ago • 2 comments

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.

fw623 avatar Sep 03 '20 10:09 fw623

Please use a proper issue template:

image

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.

MichalLytek avatar Sep 03 '20 10:09 MichalLytek

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.

fw623 avatar Sep 03 '20 11:09 fw623