this.connection.getMetadata is not a function

827 Views Asked by At

I have a base graphql resolver that looks like this:

import { ClassType, Resolver, Query, UseMiddleware, Mutation, Arg } from 'type-graphql';
import { InjectRepository, OrmManager } from 'typeorm-typedi-extensions';
import { EntityManager } from 'typeorm';
import 'reflect-metadata';
// import { Repository } from 'typeorm';

function baseResolver<
T extends ClassType,
X extends ClassType,
P extends ClassType
>(
    suffix: string,
    returnType: T,
    inputType: X,
    repository: any,
    updateType?: P
): any {
  @Resolver({ isAbstract: true })
    abstract class BaseResolver {
    protected repo: any;

    constructor(@OrmManager() protected entityManager: EntityManager) {
      this.repo = entityManager.getCustomRepository(repository);
    }

    @Query(() => [returnType], { name: `getAll${suffix}` })
    async getAll(): Promise<T[]> {
      return this.repo.find();
    }

    @Mutation(() => returnType, { name: `create${suffix}` })
    async create(@Arg('data', () => inputType) data: any): Promise<T> {
      const result = this.repo.create(data).save();
      return result;
    }

    @Mutation(() => Boolean, { name: `update${suffix}` })
    async update(@Arg('data', () => updateType) data: any) {
      await this.repo.update(data.id, data);
      return true;
    }

    @Mutation(() => Boolean, { name: `delete${suffix}` })
    async delete(@Arg('id') id: string) {
      await this.repo.softDelete(id);
    }
  }

  return BaseResolver;
}

export default baseResolver;

I then extend the resolver in other descendant resolvers. When I try to run this, I get the following error(thrown by the constructor):

 "TypeError: this.connection.getMetadata is not a function",

Can anyone help with this? What could be the issue with this code?

1

There are 1 best solutions below

0
Marcel Gruber On

Try this:

npm install 'reflect-metadata' -D

Add this to your tsconfig.json:

"compilerOptions": {
  "types": ["reflect-metadata"],
  "experimentalDecorators": true

Restart VS Code