MichalLytek / MichalLytek/type-graphql
Resolver inheritance: `declarations: true` example
- Dominant language
- TypeScript
- Stars
- 8.1k
- Forks
- 672
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the issue**
The [inheritance examples](https://typegraphql.com/docs/inheritance.html#resolver-inheritance) 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`:
```typescript
export interface IBaseResolver {
getAll(first: number): Promise
}
export function createBaseResolver(
suffix: string, objectTypeCls: T
): new() => IBaseResolver {
@Resolver({ isAbstract: true })
class BaseResolver implements IBaseResolver {
protected items: T[] = [];
@Query(type => [objectTypeCls], { name: `getAll${suffix}` })
async getAll(@Arg("first", type => Int) first: number): Promise {
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`.
Contributor guide
Research direction
Start with the resolver inheritance section linked in the issue and compare its current example with the proposed IBaseResolver and createBaseResolver code. Add a concrete declarations: true example or recommendation, ensuring the example reflects the required non-abstract BaseResolver; the documentation should explain how this resolves the TypeScript error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100