I am trying to extend upon the Type-GraphQL provided example of resolvers-inheritance except replace the static data with a TypeORM repository.
Here is how the PersonResolver extends the ResourceResolver and how it passes the persons array as the second argument of the ResourceResolver constructor.
const persons: Person[] = [
{
id: 1,
name: "Person 1",
age: 23,
role: PersonRole.Normal,
},
{
id: 2,
name: "Person 2",
age: 48,
role: PersonRole.Admin,
},
];
@Resolver()
export class PersonResolver extends ResourceResolver(Person, persons) {
...
}
Inside the ResourceResolver
export function ResourceResolver<TResource extends Resource>(
ResourceCls: ClassType<TResource>,
resources: TResource[],
) {
const resourceName = ResourceCls.name.toLocaleLowerCase();
// `isAbstract` decorator option is mandatory to prevent multiple registering in schema
@Resolver(_of => ResourceCls, { isAbstract: true })
@Service()
abstract class ResourceResolverClass {
protected resourceService: ResourceService<TResource>;
constructor(factory: ResourceServiceFactory) {
this.resourceService = factory.create(resources);
}
...
}
And in the ResourceServiceFactory
@Service()
export class ResourceServiceFactory {
create<TResource extends Resource>(resources?: TResource[]) {
return new ResourceService(resources);
}
}
export class ResourceService<TResource extends Resource> {
constructor(protected resources: TResource[] = []) {}
getOne(id: number): TResource | undefined {
return this.resources.find(res => res.id === id);
}
I would like to know the best way to implement the ResourceResolver but instead of static data I would like to pass a repository from TypeORM.
Here is the original example - https://github.com/MichalLytek/type-graphql/tree/master/examples/resolvers-inheritance.
Any help or advice is greatly appreciated.