npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@codejamninja/typegraphql-nestjs

v0.2.3-0

Published

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

Downloads

10

Readme

TypeGraphQL NestJS Module

Basic integration of TypeGraphQL in NestJS.

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:

npm i typegraphql-nestjs @nestjs/graphql

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

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 like emitSchemaFile or authChecker apart from the standard GqlModuleOptions from @nestjs/graphql like installSubscriptionHandlers or context:

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:

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:

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 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, interface args and resolvers, query complexity or Prisma 2 integration.

Examples

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

  1. Basics

    Basics of the integration, like TypeGraphQLModule.forRoot usage

  2. Multiple Servers

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

  3. Request scoped dependencies

    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.