Type-graphql: Resolvers inheritance

Created on 28 May 2018  路  3Comments  路  Source: MichalLytek/type-graphql

As there's demand for that feature (#89 #47), here we come! 馃槃

  • [x] decorator option for setting schema name (allow methods use in child class)
  • [x] disable emitting in schema queries/mutations from abstract resolver classes
  • [x] detecting extending resolver class and copping super resolver metadata
  • [x] provide example code
  • [x] describe feature in docs
  • [x] test all the new things 馃槈
Enhancement

Most helpful comment

I've been away from JS for a minute.... just saw this... can't wait to use it. :) . Hope it didn't make you cringe too much implementing it.. your implementation of it looks great!

All 3 comments

I've been away from JS for a minute.... just saw this... can't wait to use it. :) . Hope it didn't make you cringe too much implementing it.. your implementation of it looks great!

Hi @MichalLytek,
I followed this pattern to write base resolver. When I wanted to use it in another project using type-graphql, i moved all the code to a new npm package. But I am getting the following error when running typescript compiler

Return type of exported function has or is using private name 'BaseResolverClass'.
Here's my code for reference

export function BaseResolver<T>(Entity: {
  new (): T;
}) {
  const entityName = Entity.name;

  @injectable()
  @Resolver({ isAbstract: true })
  class BaseResolverClass {
    repository: Repository<T>;
    constructor(private dbConnection: Promise<Connection>) {
      dbConnection
        .then(connection => connection.getRepository(Entity))
        .then(repository => {
          this.repository = repository;
        });
    }

    @Mutation(() => Entity, { name: `create${entityName}` })
    async create(
      @Arg('entry', type => Entity)
      entry: DeepPartial<T>,
    ): Promise<T> {
      const entity = new Entity();
      Object.assign(entity, entry);
      return await this.repository.save(entity);
    }

    @Query(returns => Entity, { name: `find${entityName}` })
    async findOneById(
      @Arg('id', type => Int)
      id: number,
    ): Promise<T> {
      return this.repository.findOneOrFail(id);
    }

    @Mutation(() => Entity, { name: `update${entityName}` })
    async update(
      @Arg('id', type => Int)
      id: number,
      @Arg('entry', type => Entity)
      partialEntry: DeepPartial<T>,
    ): Promise<T> {
      const foundEntity = await this.findOneById(id);
      this.repository.merge(foundEntity, partialEntry);
      return await this.repository.save(foundEntity);
    }
  }

  return BaseResolverClass
}

This is because I am exporting declaration from this package to be used in my 2 projects. Is there any way to get around this?

@itsrimzz1
Yes, you have to redeclare the shape of the inner class, because their types are not explicit, so it can't generate proper *.d.ts typings.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tybot204 picture Tybot204  路  3Comments

tafelito picture tafelito  路  3Comments

Janushan picture Janushan  路  3Comments

glentakahashi picture glentakahashi  路  3Comments

avkonst picture avkonst  路  3Comments