{"id":14978655,"url":"https://github.com/michallytek/typegraphql-nestjs","last_synced_at":"2025-04-05T22:05:27.590Z","repository":{"id":38427019,"uuid":"251112342","full_name":"MichalLytek/typegraphql-nestjs","owner":"MichalLytek","description":"TypeGraphQL integration with NestJS","archived":false,"fork":false,"pushed_at":"2024-02-14T09:46:16.000Z","size":745,"stargazers_count":139,"open_issues_count":3,"forks_count":19,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-23T20:05:18.846Z","etag":null,"topics":["graphql","nest","nestjs","type-graphql","typegraphql"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MichalLytek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-29T19:11:48.000Z","updated_at":"2024-06-18T19:46:57.510Z","dependencies_parsed_at":"2024-06-18T19:46:45.760Z","dependency_job_id":"5401f340-b8eb-443c-a491-0e8ba6e27786","html_url":"https://github.com/MichalLytek/typegraphql-nestjs","commit_stats":{"total_commits":41,"total_committers":3,"mean_commits":"13.666666666666666","dds":0.04878048780487809,"last_synced_commit":"8f9bc06e443321b0187437f7d1aef7ce17bd92c5"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichalLytek%2Ftypegraphql-nestjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichalLytek%2Ftypegraphql-nestjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichalLytek%2Ftypegraphql-nestjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichalLytek%2Ftypegraphql-nestjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MichalLytek","download_url":"https://codeload.github.com/MichalLytek/typegraphql-nestjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247406085,"owners_count":20933803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","nest","nestjs","type-graphql","typegraphql"],"created_at":"2024-09-24T13:58:08.611Z","updated_at":"2025-04-05T22:05:27.570Z","avatar_url":"https://github.com/MichalLytek.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"typegraphql logo\" src=\"https://raw.githubusercontent.com/MichalLytek/typegraphql-nestjs/master/typegraphql-logo.png\" width=\"300\" height=\"200\"\u003e\n  \u003cimg alt=\"nest logo\" src=\"https://nestjs.com/img/logo_text.svg\" width=\"300\" height=\"200\"\u003e\n\u003c/p\u003e\n\n# TypeGraphQL NestJS Module\n\nBasic integration of [TypeGraphQL](https://typegraphql.com/) in [NestJS](https://nestjs.com/).\n\nAllows to use TypeGraphQL features while integrating with NestJS modules system and dependency injector.\n\n## Installation\n\nFirst, you need to instal the `typegraphql-nestjs` module along with `@nestjs/graphql`:\n\n```sh\nnpm i typegraphql-nestjs @nestjs/graphql\n```\n\nIf you haven't installed it yet, it's time to add `type-graphql` into the project:\n\n```sh\nnpm i type-graphql\n```\n\n## How to use?\n\nThe `typegraphql-nestjs` package exports `TypeGraphQLModule` dynamic module, which is based on the official NestJS `GraphQLModule`. It exposes three static methods:\n\n### `.forRoot()`\n\nThe first one is `TypeGraphQLModule.forRoot()` which you should call on your root module, just like with the official `GraphQLModule`.\n\nThe only difference is that as its argument you can provide [typical TypeGraphQL `buildSchema` options](https://typegraphql.com/docs/bootstrap.html) like `emitSchemaFile` or `authChecker` apart from the [standard `GqlModuleOptions` from `@nestjs/graphql`](https://docs.nestjs.com/graphql/quick-start#installation) like `installSubscriptionHandlers` or `context`:\n\n```ts\nimport { Module } from \"@nestjs/common\";\nimport { TypeGraphQLModule } from \"typegraphql-nestjs\";\n\nimport RecipeModule from \"./recipe/module\";\nimport { authChecker } from \"./auth\";\n\n@Module({\n  imports: [\n    TypeGraphQLModule.forRoot({\n      driver: ApolloDriver,\n      emitSchemaFile: true,\n      authChecker,\n      scalarsMap: [{ type: Date, scalar: GraphQLTimestamp }],\n      context: ({ req }) =\u003e ({ currentUser: req.user }),\n    }),\n    RecipeModule,\n  ],\n})\nexport default class AppModule {}\n```\n\nThen, inside the imported modules (like `RecipeModule`) you just need to register the resolvers classes in the module `providers` array:\n\n```ts\nimport { Module } from \"@nestjs/common\";\n\nimport RecipeResolver from \"./resolver\";\nimport RecipeService from \"./service\";\n\n@Module({\n  providers: [RecipeResolver, RecipeService],\n})\nexport default class RecipeModule {}\n```\n\nAnd that's it! 😁\n\nNotice that the resolvers classes are automatically inferred from your submodules `providers` array, so you don't need to specify `resolvers` property from TypeGraphQL `buildSchema` options inside `TypeGraphQLModule.forRoot()`.\n\n### `.forFeature()`\n\nIn case of need to provide `orphanedTypes` setting, you should use `TypeGraphQLModule.forFeature()`. The recommended place for that is in the module where the orphaned type (like `SuperRecipe`) belongs:\n\n```ts\nimport { Module } from \"@nestjs/common\";\nimport { TypeGraphQLModule } from \"typegraphql-nestjs\";\n\nimport RecipeResolver from \"./resolver\";\nimport RecipeService from \"./service\";\nimport { SuperRecipe } from \"./types\";\n\n@Module({\n  imports: [\n    TypeGraphQLModule.forFeature({\n      orphanedTypes: [SuperRecipe],\n    }),\n  ],\n  providers: [RecipeResolver, RecipeService],\n})\nexport default class RecipeModule {}\n```\n\nUsing `.forFeature()` ensures proper schemas isolation and automatically supply `orphanedTypes` option for underlying `buildSchema` from TypeGraphQL - again, there's no need to provide it manually in `.forRoot()` options.\n\n### `.forRootAsync()`\n\nIf you need to access some services to construct the `TypeGraphQLModule` options, you might be interested in the `TypeGraphQLModule.forRootAsync()` method. It allows you to define your own `useFactory` implementation where you have injected services from `imports` option.\n\nExample of using the config service to generate `TypeGraphQLModule` options:\n\n```ts\n@Module({\n  imports: [\n    ConfigModule,\n    RecipeModule,\n    TypeGraphQLModule.forRootAsync({\n      driver: ApolloDriver,\n      inject: [ConfigService],\n      useFactory: async (config: ConfigService) =\u003e ({\n        cors: true,\n        debug: config.isDevelopmentMode,\n        playground: !config.isDevelopmentMode,\n        scalarsMap: [{ type: Date, scalar: GraphQLTimestamp }],\n        emitSchemaFile:\n          config.isDevelopmentMode \u0026\u0026 path.resolve(__dirname, \"schema.graphql\"),\n      }),\n    }),\n  ],\n})\nexport default class AppModule {}\n```\n\n### Apollo Federation\n\n`typegraphql-nestjs` has also support for [Apollo Federation](https://www.apollographql.com/docs/federation/).\n\nHowever, Apollo Federation requires building a federated GraphQL schema, hence you need to adjust your code a bit.\n\nThe usage is really similar to the basic case - the only difference is that in `TypeGraphQLModule.forFeature()` method you can provide a `referenceResolvers` option object, which is needed in some cases of Apollo Federation:\n\n```ts\nfunction resolveUserReference(\n  reference: Pick\u003cUser, \"id\"\u003e,\n): Promise\u003cUser | undefined\u003e {\n  return db.users.find({ id: reference.id });\n}\n\n@Module({\n  imports: [\n    TypeGraphQLModule.forFeature({\n      orphanedTypes: [User],\n      referenceResolvers: {\n        User: {\n          __resolveReference: resolveUserReference,\n        },\n      },\n    }),\n  ],\n  providers: [AccountsResolver],\n})\nexport default class AccountModule {}\n```\n\nFor the `.forRoot()` method there's no differences - just need to provide `driver: ApolloFederationDriver` option in order to build a subgraph schema, same as with `GraphQLModule` from `@nestjs/graphql` described in the [NestJS docs](https://docs.nestjs.com/graphql/federation). However, you also need to explicitly setup federation version, by using `federationVersion` option:\n\n```ts\n@Module({\n  imports: [\n    TypeGraphQLModule.forRoot({\n      driver: ApolloFederationDriver,\n      federationVersion: 2,\n    }),\n    AccountModule,\n  ],\n})\nexport default class AppModule {}\n```\n\nThen, for exposing the federated schema using Apollo Gateway, you should use the standard [NestJS `ApolloGatewayDriver` solution](https://docs.nestjs.com/graphql/federation#federated-example-gateway).\n\n## Caveats\n\nWhile this integration provides a way to use TypeGraphQL with NestJS modules and dependency injector, for now it doesn't support [other NestJS features](https://docs.nestjs.com/graphql/tooling) like guards, interceptors, filters and pipes.\n\nTo achieve the same goals, you can use standard TypeGraphQL equivalents - middlewares, custom decorators, built-in authorization and validation.\n\nMoreover, with `typegraphql-nestjs` you can also take advantage of additional features (comparing to `@nestjs/graphql`) like [inline field resolvers](https://typegraphql.com/docs/resolvers.html#field-resolvers), [Prisma 2 integration](https://github.com/MichalLytek/typegraphql-prisma/blob/main/Readme.md) or up-to-date capabilities like deprecating input fields and args (thanks to always synced with latests `graphql-js`).\n\n## Examples\n\nYou can see some examples of the integration in this repo:\n\n1. [Basics](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/1-basics)\n\n   Basics of the integration, like `TypeGraphQLModule.forRoot` usage\n\n1. [Multiple Servers](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/2-multiple-servers)\n\n   Advanced usage of multiple schemas inside single NestJS app - demonstration of schema isolation in modules and `TypeGraphQLModule.forFeature` usage\n\n1. [Request scoped dependencies](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/3-request-scoped)\n\n   Usage of request scoped dependencies - retrieving fresh instances of resolver and service classes on every request (query/mutation)\n\n1. [Middlewares](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/4-middlewares)\n\n   Usage of class-based middlewares - modules, providers and schema options\n\n1. [Apollo Federation (OLD)](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/5-federation)\n\n   Showcase of the legacy Apollo Federation approach\n\n1. [Apollo Federation V2](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/6-federation-2)\n\n   Showcase of the new Apollo Federation V2 approach\n\nMost of them you can run by using `ts-node`, like `npx ts-node ./examples/1-basics/index.ts`.\n\nAll examples folders contain a `query.graphql` file with some examples operations you can perform on the GraphQL servers.\n\n## Security contact information\n\nTo report a security vulnerability, please use the\n[Tidelift security contact](https://tidelift.com/security).\nTidelift will coordinate the fix and disclosure.\n","funding_links":["https://tidelift.com/security"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichallytek%2Ftypegraphql-nestjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichallytek%2Ftypegraphql-nestjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichallytek%2Ftypegraphql-nestjs/lists"}