MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
https://typegraphql.com
MIT License
8.04k stars 678 forks source link

Should able to use TypeGraphQL with Tsyringe #1398

Closed leandroluk closed 1 year ago

leandroluk commented 1 year ago

Describe the issue I have a project using DI with tsyringe and i should to add graphql using type-graphql, but in documentation is only listed the TypeDI as a dependency injection lib

Are you able to make a PR that fix this? If is possible to use Tsyringe, plesse add some example in doc's

Additional context N/A

MichalLytek commented 1 year ago

Tsyringe works well for TypeGraphQL projects. What have you tried and what was not working for you?

carlocorradini commented 1 year ago

Yes, it's possible! \ Most IOC containers have the get function to retrieve an instance of the given class type. tsyringe instead have the resolve function. \ Therefore, you have to change the container option when building the schema to instruct type-graphql how to retrieve (get) a class instance from a class type:

import { container } from 'tsyringe';

const schema = await buildSchema({
  // ...
  container: { get: (cls) => container.resolve(cls) }
});
carlocorradini commented 1 year ago

@leandroluk Can we close this?

fknop commented 1 year ago

Yes, it's possible! Most IOC containers have the get function to retrieve an instance of the given class type. tsyringe instead have the resolve function. Therefore, you have to change the container option when building the schema to instruct type-graphql how to retrieve (get) a class instance from a class type:

import { container } from 'tsyringe';

const schema = await buildSchema({
  // ...
  container: { get: (cls) => container.resolve(cls) }
});

I can confirm this works, using tsyringe and typegraphql in a large production project :)

clayrisser commented 10 months ago

How to register the resolvers with tsringe? I get the following error.

TypeInfo not known for "AuthResolver"
carlocorradini commented 10 months ago

@clayrisser See tsyringe example