'TypeORM EntityMetadataNotFoundError: No metadata for \"Score\" was found

My project has 2 seperate connection datasources. One is to a readonly mysql database, the other is a postgres database.

Everything on the mysql database works fine. But I'm having issues with the postgres database.

Whenever I run the createQueryBuilder to getMany I get the following error.

EntityMetadataNotFoundError: No metadata for \"Score\" was found.

In my index.ts file where I spin up my server, I initialize the postgres db datasouce

pgDataSource.initialize();

And here is pgDataSource

import 'reflect-metadata';
import 'dotenv-safe/config';
import { DataSource } from 'typeorm';
import path from 'path';
import Score from '../entities/Score';

const pgDataSource = new DataSource({
  name: 'favourites',
  type: 'postgres',
  url: process.env.DATABASE_URL,
  logging: true,
  synchronize: true,
  migrations: [path.join(__dirname, './migrations/*')],
  entities: [Score],
});

export const Manager = pgDataSource.manager;
export const ScoreRepository = pgDataSource.getRepository(Score);

export default pgDataSource;

At this point, everything seems fine, it creates a table in my database with the Score entity, so the connection exists and it acknowledges that there is an Entity as it creates the corresponding table.

Here is my Score entity

import { ObjectType, Field, Int } from 'type-graphql';
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from 'typeorm';

@ObjectType()
@Entity({ database: 'pp_favourites', name: 'score' })
class Score extends BaseEntity {
  @Field()
  @PrimaryGeneratedColumn()
  id!: number;

  @Field(() => Int)
  @Column()
  propertyId!: number;
}

export default Score;

I have a GraphQL resolver for the mysql database and a resolver for postgres database.

However, when I run this query

await ScoreRepository.createQueryBuilder().getMany()

In a Query decorator function, I get the No metadata for \"Score\" was found. error

Although, if I run it through a useMiddleware decorator, it strangely works?

This is what my mysql resolver looks like with the query on the ScoreRespository

import { Resolver, Query } from 'type-graphql';
import Shortlist from '../entities/Shortlist';
import { ShortlistRepository } from '../datasources/ppDataSource';
import Property from '../entities/Property';
import { ScoreRepository } from '../datasources/pgDataSource';

@Resolver(Shortlist)
class ShortlistResolver {
  @Query(() => [Property], { nullable: true })
  async getShortlist(): Promise<Property[]> {
    await ScoreRepository.createQueryBuilder().getMany()
  }
}

export default ShortlistResolver;

Perhaps, also worth mentioning, as this could potentially be the issue. But in my index file where i spin up my server. I setup my ApolloServer which uses the now deprecated getConnectionfunction from typeorm

const apolloServer = new ApolloServer({
  schema: await buildSchema({
    resolvers: [ShortlistResolver, ScoreResolver],
    validate: true,
  }),
  context: ({ req, res }) => ({
    req,
    res,
  }),
  plugins: [
    // dataloaders with TypeormLoader in type-graphql-dataloader
    ApolloServerLoaderPlugin({
      typeormGetConnection: getConnection, // for use with TypeORM
    }),
    // use older playground
    ApolloServerPluginLandingPageGraphQLPlayground({
      settings: {
        'request.credentials': 'include',
      },
    }),
  ],
});

I've tried

  1. Adding @Resolver(Score) as a decorator to the ShortlistResolver
  2. Removing the dist folder
  3. creating a new database and new connection
  4. Logged Manager.connection.entityMetadatas from pgDataSource which shows there are no entity's, despite being defined.

Any help would be greatly appreciated. I don't really know if the issue lies with TypeORM or GraphQL.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source