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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@relab/nestjs-graphql

v1.3.2

Published

GraphQL infrastructure for Nest.js

Readme

@relab/nestjs-graphql

GraphQL infrastructure utilities and plugins for NestJS applications. This package provides decorators, helpers, and Apollo Server plugins to simplify and enhance GraphQL development, including batch loading, field selection, mapping, and observability.

Features

  • Batch loading decorator for efficient DataLoader integration in resolvers
  • Field selection decorator to extract requested fields from GraphQL queries
  • Mapper and MapperSelect decorators for type-safe mapping and field selection (e.g., for Prisma)
  • Apollo Server plugins:
    • GraphiQL Playground integration
    • HTTP status code propagation for exceptions
    • Prometheus request metrics for queries and mutations

Installation

pnpm add @relab/nestjs-graphql
# or
yarn add @relab/nestjs-graphql
# or
npm install @relab/nestjs-graphql

Note: This package requires @nestjs/common, @nestjs/core, @nestjs/graphql, @nestjs/apollo, graphql, and (optionally) prom-client for metrics. Please ensure you use compatible versions with your NestJS setup.

Usage

Batch Loading with DataLoader

import { Resolver, ResolveField, Parent } from '@nestjs/graphql';
import { GraphQLResolveFieldBatch, BatchLoader } from '@relab/nestjs-graphql';

@Resolver(() => Company)
export class CompanyResolver {
  @ResolveField(() => [BankAccount])
  bankAccounts(
    @Parent() company: Company,
    @GraphQLResolveFieldBatch() batch: BatchLoader<number, BankAccount>
  ): Promise<BankAccount[]> {
    return batch(async (companyIds, fields) => {
      // Implement batch loading logic here
    }).load(company.id);
  }
}

Field Selection Decorator

import { GraphQLFields } from '@relab/nestjs-graphql';

@Query(() => Company)
getCompany(@GraphQLFields() fields: string[]) {
  // 'fields' contains the requested fields in the query
}

Mapper and MapperSelect Decorators

import { Mapper, MapperSelect } from '@relab/nestjs-graphql';

@Mapper<DbCompany, Company>(db => ({
  id: db.id,
  name: db.name,
}))
@MapperSelect<Company, any>(fields => ({
  id: true,
  name: fields.includes('name'),
}))
@ObjectType()
export class Company {
  id: number;
  name: string;
}

Advanced: Accessing Registered Mappers

You can retrieve the registered mapper or select function for a class using the following helpers:

import { getMapper, getMapperSelect } from '@relab/nestjs-graphql';

const companyMapper = getMapper<DbCompany, Company>(Company);
const companySelect = getMapperSelect<Company, any>(Company);

Apollo Server Plugins

  • GraphiQLPlaygroundPlugin: Adds a modern GraphiQL playground to your server.
  • HttpStatusCodePlugin: Propagates HTTP status codes from thrown HttpExceptions to GraphQL responses.
  • RequestMetricsPlugin: Exposes Prometheus metrics for GraphQL queries and mutations (requires prom-client).

Example Plugin Registration (Apollo Server)

Register plugins in the Apollo Server configuration:

import { ApolloDriverConfig, ApolloDriver } from '@nestjs/apollo';
import { GraphQLModule } from '@nestjs/graphql';
import { GraphiQLPlaygroundPlugin, HttpStatusCodePlugin, RequestMetricsPlugin } from '@relab/nestjs-graphql';

@Module({
  imports: [
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      plugins: [
        new RequestMetricsPlugin(),
        new HttpStatusCodePlugin(),
        GraphiQLPlaygroundPlugin.forRoot({ title: 'My API Playground' }).useFactory(),
      ],
    }),
  ],
})
export class AppModule {}

API Reference

Exports

| Export | Description | |-------------------------------|-----------------------------------------------------------------------------| | BatchLoader | Type for batch loading function for resolvers | | GraphQLResolveFieldBatch | Decorator for batch loading in resolvers | | GraphQLField | Type for GraphQL field names | | GraphQLFields | Decorator to extract requested fields from GraphQL queries | | Mapper | Decorator to register a mapping function for a class | | MapperFunction | Type for mapping function | | getMapper | Helper to retrieve a registered mapper | | MapperSelect | Decorator to register a select/include function for a class | | MapperSelectFunction | Type for select/include function | | getMapperSelect | Helper to retrieve a registered select/include function | | GraphiQLPlaygroundPlugin | Apollo Server plugin for GraphiQL playground | | GraphiQLOptions | Options for GraphiQL playground | | HttpStatusCodePlugin | Apollo Server plugin for HTTP status code propagation | | RequestMetricsPlugin | Apollo Server plugin for Prometheus request metrics |

See the source code and TypeScript types for detailed API documentation and usage examples for each decorator and plugin.

License

MIT