# @codejamninja/typegraphql-nestjs

> Basic integration of TypeGraphQL in NestJS. Allows to use TypeGraphQL features while integrating with NestJS modules system and dependency injector.

Latest version **0.2.3-0** (published 2020-09-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @codejamninja/typegraphql-nestjs
pnpm add @codejamninja/typegraphql-nestjs
yarn add @codejamninja/typegraphql-nestjs
bun add @codejamninja/typegraphql-nestjs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3-0 |
| Published | 2020-09-22 |
| First published | 2020-09-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 10.3 |
| Dependencies | 1 |
| Unpacked size | 16.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Michał Lytek |
| Maintainers | codejamninja |
| Keywords | type-graphql, nest, nestjs, graphql |

## Links

- npm: https://www.npmjs.com/package/@codejamninja/typegraphql-nestjs
- Repository: https://github.com/MichalLytek/typegraphql-nestjs
- Homepage: https://github.com/MichalLytek/typegraphql-nestjs#readme
- Issues: https://github.com/MichalLytek/typegraphql-nestjs/issues
- Funding: https://opencollective.com/typegraphql
- npm.io page: https://npm.io/package/@codejamninja/typegraphql-nestjs

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.0.1

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.2.3-0 (latest) — 2020-09-22

## README

<p align="center">
  <img alt="typegraphql logo" src="https://raw.githubusercontent.com/MichalLytek/typegraphql-nestjs/master/typegraphql-logo.png" width="300" height="200">
  <img alt="nest logo" src="https://nestjs.com/img/logo_text.svg" width="300" height="200">
</p>

# TypeGraphQL NestJS Module

Basic integration of [TypeGraphQL](https://typegraphql.com/) in [NestJS](https://nestjs.com/).

Allows to use TypeGraphQL features while integrating with NestJS modules system and dependency injector.

## Installation

First, you need to instal the `typegraphql-nestjs` module along with `@nestjs/graphql`:

```sh
npm i typegraphql-nestjs @nestjs/graphql
```

If you haven't installed it yet, it's time to add `type-graphql` into the project:

```sh
npm i type-graphql
```

## How to use?

The `typegraphql-nestjs` package exports `TypeGraphQLModule` dynamic module, which is based on the official NestJS `GraphQLModule`.

It exposes two static methods. The first one is `TypeGraphQLModule.forRoot()` which you should call on your root module, just like with `GraphQLModule`.

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`:

```ts
import { Module } from "@nestjs/common";
import { TypeGraphQLModule } from "typegraphql-nestjs";

import RecipeModule from "./recipe/module";
import { authChecker } from "./auth";

@Module({
  imports: [
    TypeGraphQLModule.forRoot({
      emitSchemaFile: true,
      validate: false,
      authChecker,
      dateScalarMode: "timestamp",
      context: ({ req }) => ({ currentUser: req.user }),
    }),
    RecipeModule,
  ],
})
export default class AppModule {}
```

Then, inside the imported modules (like `RecipeModule`) you just need to register the resolvers classes in the module `providers` array:

```ts
import { Module } from "@nestjs/common";

import RecipeResolver from "./resolver";
import RecipeService from "./service";

@Module({
  providers: [RecipeResolver, RecipeService],
})
export default class RecipeModule {}
```

And that's it! 😁

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()`.

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:

```ts
import { Module } from "@nestjs/common";
import { TypeGraphQLModule } from "typegraphql-nestjs";

import RecipeResolver from "./resolver";
import RecipeService from "./service";
import { SuperRecipe } from "./types";

@Module({
  imports: [
    TypeGraphQLModule.forFeature({
      orphanedTypes: [SuperRecipe],
    }),
  ],
  providers: [RecipeResolver, RecipeService],
})
export default class RecipeModule {}
```

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.

## Caveats

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.

To achieve the same goals, you can use standard TypeGraphQL equivalents - middlewares, custom decorators, built-in authorization and validation.

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).

## Examples

You can see some examples of the integration in this repo:

1. [Basics](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/1-basics)

   Basics of the integration, like `TypeGraphQLModule.forRoot` usage

1. [Multiple Servers](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/2-multiple-servers)

   Advanced usage of multiple schemas inside single NestJS app - demonstration of schema isolation in modules and `TypeGraphQLModule.forFeature` usage

1. [Request scoped dependencies](https://github.com/MichalLytek/typegraphql-nestjs/tree/master/examples/3-request-scoped)

   Usage of request scoped dependencies - retrieving fresh instances of resolver and service classes on every request (query/mutation)

You can run them by using `ts-node`, like `npx ts-node ./examples/1-basics/index.ts`.

All examples folders contain a `query.gql` file with some examples operations you can perform on the GraphQL servers.

---
_Source: https://npm.io/package/@codejamninja/typegraphql-nestjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
