简体   繁体   中英

How to use TypeGraphQL with tsyringe

I have a project using tsyringe and a RESTFul API. Now i should to add a GraphQL API using the type-graphql but in our documentation no have some example using it.

Is possiblem to use tsyringe to make DI with TypeGraphQL?

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) }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM