type-graphql icon indicating copy to clipboard operation
type-graphql copied to clipboard

Typeorm breaking changes in 0.3.0+ affecting TypeGraphql examples

Open nelsonfleig opened this issue 2 years ago • 5 comments

Describe the issue Typeorm 0.3.0+ has released with many breaking changes for the Typegraphql examples. Notably, all container-related features in Typeorm have been depecreated affecting how TypeDI is used. Also the way to establish a typeorm connection to a DB with Typeorm.createConnection() has been depecrated in favour of Typeorm.DataSource.

At a first glance, the following examples have been affected:

typeorm-basic-usage typeorm-lazy-relations

Are you able to make a PR that fix this? Not currently

nelsonfleig avatar Mar 28 '22 02:03 nelsonfleig

Has anyone managed to upgrade Typeorm to 0.3.0 with TypeGraphql?

laukaichung avatar Jul 09 '22 06:07 laukaichung

Has anyone managed to upgrade Typeorm to 0.3.0 with TypeGraphql?

Yes, @laukaichung . I'm currently working on a small project and I'm using TypeORM 0.3.6 with TypeGraphQL 1.1.1 I'm using both TypeDi for injection, as I'm using the repository pattern, as well as multiple data-sources (as I have a DS for a DB I only use for tests).

Do you have a concrete issue? You can check my project out, if you'd like. All basic examples work. You can also look at specific commits, as I work incrementally. It's not much, but it's honest work 🙃

I have only tried MySQL DB, but should work just as well with PostgreSql. Haven't worked with NoSql DBs though, so cannot comment on that.

Cry0nicS avatar Jul 15 '22 16:07 Cry0nicS

Has anyone managed to upgrade Typeorm to 0.3.0 with TypeGraphql?

Yes, @laukaichung . I'm currently working on a small project and I'm using TypeORM 0.3.6 with TypeGraphQL 1.1.1 I'm using both TypeDi for injection, as I'm using the repository pattern, as well as multiple data-sources (as I have a DS for a DB I only use for tests).

Do you have a concrete issue? You can check my project out, if you'd like. All basic examples work. You can also look at specific commits, as I work incrementally. It's not much, but it's honest work upside_down_face

I have only tried MySQL DB, but should work just as well with PostgreSql. Haven't worked with NoSql DBs though, so cannot comment on that.

Thank you for the example. Working fine so far. I guess typeorm-typedi-extensions isn't compatible because the set isn't exposed so I can't do Container.set(DataSource, dataSource); after the data source initialization.

Also, all of my resolvers need to be decorated with Service().

My resolver looks like this:

import { Service } from "typedi";
import { AppDataSource } from "../config/data-source";
import {  Args, Mutation, Resolver} from "type-graphql";

@Service()
@Resolver(() => User)
class UserResolver {

  // Won't work anymore because typeorm-typedi-extensions is uninstalled.
  // constructor(
  //   @InjectRepository(User) private repository: Repository<User>,
  // ) {}

 private readonly repository = AppDataSource.getRepository(User);

 @Mutation((returnType) => User)
 public create(@Args() args){
   this.repository.save(...args)
 }
}

If you are using type-graphql-dataloader you need to change its config because the typeorm.getConnection() is depreciated:

import { ApolloServer } from "apollo-server-express"; // Must be v3+
import { AppDataSource } from "../config/data-source";
import { ApolloServerLoaderPlugin } from "type-graphql-dataloader";

async function startServer(){
  await AppDataSource.initialize()

  const apollo = new ApolloServer({
    schema,
    plugins: [
      ApolloServerLoaderPlugin({
        typeormGetConnection: ()=> AppDataSource
      }),
    ],
  });
}


startServer()

laukaichung avatar Aug 18 '22 09:08 laukaichung

typeorm-typedi-extensions

Why do you need this? Last update was 2 years ago... I've just used TypeDI. Should be enough.

Also, all of my resolvers need to be decorated with Service().

Yup

You can also use the @Inject decorator (see the docs). I just got used to do things the "old way"

Cry0nicS avatar Aug 18 '22 09:08 Cry0nicS

I guess the examples should be updates targeting mostly the latest version. This way it would avoid such errors related to previous/unmaintained dependency versions.

rafaell-lycan avatar Nov 01 '22 12:11 rafaell-lycan