|
| 1 | +<p align="center"> |
| 2 | + <img alt="typegraphql logo" src="https://raw.githubusercontent.com/MichalLytek/typegraphql-nestjs/master/typegraphql-logo.png" width="300" height="200"> |
| 3 | + <img alt="nest logo" src="https://nestjs.com/img/logo_text.svg" width="300" height="200"> |
| 4 | +</p> |
| 5 | + |
| 6 | +# TypeGraphQL NestJS Module |
| 7 | + |
| 8 | +Basic integration of [TypeGraphQL](https://typegraphql.com/) in [NestJS](https://nestjs.com/). |
| 9 | + |
| 10 | +Allows to use TypeGraphQL features while integrating with NestJS modules system and dependency injector. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```sh |
| 15 | +npm i typegraphql-nestjs @nestjs/graphql |
| 16 | +``` |
| 17 | + |
| 18 | +or |
| 19 | + |
| 20 | +```sh |
| 21 | +yarn add typegraphql-nestjs @nestjs/graphql |
| 22 | +``` |
| 23 | + |
| 24 | +## How to use? |
| 25 | + |
| 26 | +The `typegraphql-nestjs` package exports `TypeGraphQLModule` dynamic module, which is based on the official NestJS `GraphQLModule`. |
| 27 | + |
| 28 | +It exposes two static methods. The first one is `TypeGraphQLModule.forRoot()` which you should call on your root module, just like with `GraphQLModule`. |
| 29 | + |
| 30 | +The 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`: |
| 31 | + |
| 32 | +```ts |
| 33 | +import { Module } from "@nestjs/common"; |
| 34 | +import { TypeGraphQLModule } from "typegraphql-nestjs"; |
| 35 | + |
| 36 | +import RecipeModule from "./recipe/module"; |
| 37 | +import { authChecker } from "./auth"; |
| 38 | + |
| 39 | +@Module({ |
| 40 | + imports: [ |
| 41 | + TypeGraphQLModule.forRoot({ |
| 42 | + emitSchemaFile: true, |
| 43 | + validate: false, |
| 44 | + authChecker, |
| 45 | + dateScalarMode: "timestamp", |
| 46 | + context: ({ req }) => ({ currentUser: req.user }), |
| 47 | + }), |
| 48 | + RecipeModule, |
| 49 | + ], |
| 50 | +}) |
| 51 | +export default class AppModule {} |
| 52 | +``` |
| 53 | + |
| 54 | +Then, inside the imported modules (like `RecipeModule`) you just need to register the resolvers classes in the module `providers` array: |
| 55 | + |
| 56 | +```ts |
| 57 | +import { Module } from "@nestjs/common"; |
| 58 | + |
| 59 | +import RecipeResolver from "./resolver"; |
| 60 | +import RecipeService from "./service"; |
| 61 | + |
| 62 | +@Module({ |
| 63 | + providers: [RecipeResolver, RecipeService], |
| 64 | +}) |
| 65 | +export default class RecipeModule {} |
| 66 | +``` |
| 67 | + |
| 68 | +And that's it! 😁 |
| 69 | + |
| 70 | +Notice 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()`. |
| 71 | + |
| 72 | +In 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: |
| 73 | + |
| 74 | +```ts |
| 75 | +import { Module } from "@nestjs/common"; |
| 76 | +import { TypeGraphQLModule } from "typegraphql-nestjs"; |
| 77 | + |
| 78 | +import RecipeResolver from "./resolver"; |
| 79 | +import RecipeService from "./service"; |
| 80 | +import { SuperRecipe } from "./types"; |
| 81 | + |
| 82 | +@Module({ |
| 83 | + imports: [ |
| 84 | + TypeGraphQLModule.forFeature({ |
| 85 | + orphanedTypes: [SuperRecipe], |
| 86 | + }), |
| 87 | + ], |
| 88 | + providers: [RecipeResolver, RecipeService], |
| 89 | +}) |
| 90 | +export default class RecipeModule {} |
| 91 | +``` |
| 92 | + |
| 93 | +Using `.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. |
| 94 | + |
| 95 | +## Caveats |
| 96 | + |
| 97 | +While 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. |
| 98 | + |
| 99 | +To achieve the same goals, you can use standard TypeGraphQL equivalents - middlewares, custom decorators, built-in authorization and validation. |
| 100 | + |
| 101 | +Moreover, 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), [interface args and resolvers](https://typegraphql.com/docs/next/interfaces.html#resolvers-and-arguments), [query complexity](https://typegraphql.com/docs/complexity.html) or [Prisma 2 integration](https://github.com/MichalLytek/type-graphql/blob/prisma/Readme.md). |
| 102 | + |
| 103 | +## Examples |
| 104 | + |
| 105 | +You can see some examples of the integration in this repo: |
| 106 | + |
| 107 | +1. [Basics](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/1-basics) |
| 108 | + |
| 109 | + Basics of the integration, like `TypeGraphQLModule.forRoot` usage |
| 110 | + |
| 111 | +1. [Multiple Servers](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/2-multiple-servers) |
| 112 | + |
| 113 | + Advanced usage of multiple schemas inside single NestJS app - demonstration of schema isolation in modules and `TypeGraphQLModule.forFeature` usage |
| 114 | + |
| 115 | +You can run them by using `ts-node`, like `npx ts-node ./examples/1-basics/index.ts`. |
| 116 | + |
| 117 | +All folders contain a `query.graphgql` file with some examples operations you can perform on the GraphQL servers. |
0 commit comments