@golevelup/nestjs-graphql-request

中文文档
5.0.0发布于 2026/03/19MITTypeScriptCommonJS近 30 天下载 8.7万

Badass utilities for integrating graphql-request and NestJS

原始使用文档
文档目录4 节
  1. Getting Started
  2. Import
  3. Inject the GraphQLClient
  4. Typesafe GraphQL Access

GraphQL Request

This library provides the GraphQLRequestModule which makes it easy to initialize an instance of GraphQLClient for use in Depdendency Injection. This can be combined with automatic codegen for type safe access to third party GQL APIs from your Nest services

version downloads license

Getting Started

::: code-group

npm install ---save @golevelup/nestjs-graphql-request
yarn add @golevelup/nestjs-graphql-request
pnpm add @golevelup/nestjs-graphql-request

:::

Import

Import and add GraphQLRequestModule to the imports section of the consuming module.

import { GraphQLRequestModule } from '@golevelup/nestjs-graphql-request';

@Module({
  imports: [
    GraphQLRequestModule.forRoot({
      // Exposes configuration options based on the graphql-request package
      endpoint: config.get('endpoint'),
      options: {
        headers: {
          'content-type': 'application/json',
          'x-hasura-admin-secret': config.get('secret'),
        },
      },
    }),
  ],
})
export class AppModule {
  // ...
}

Inject the GraphQLClient

To make GraphQL requests from your controllers or services use the provided decorator @InjectGraphQLClient.

import { InjectGraphQLClient } from '@golevelup/nestjs-graphql-request';
import { GraphQLClient } from 'graphql-request';

@Injectable()
export class ExampleService {
  constructor(@InjectGraphQLClient() private readonly client: GraphQLClient) {}
}

Typesafe GraphQL Access

The GraphQL client works best when combined with GraphQL code generation tools for communicating to a GraphQL endpoint. Follow the GraphQL Code Generator GraphQL Request Plugin Instructions.

Running the code generator against a GraphQL Endpoint will produce typescript interfaces for all operations and types exposed by the API as well as a getSdk function which you can use in conjunction with GraphQL Request to get intellisense and type saftey for all requests you make. The getSdk function only requires a GraphQLClient instance (which this library provides). However because each generated SDK is unique per endpoint, it's up to you to wire up the provider for this in your application. This can be done using a Provider Factory with minimal code.

import {
  GraphQLRequestModule,
  GraphQLClientInject,
} from '@golevelup/nestjs-graphql-request';
import { getSdk } from './your-codegen-location';

@Module({
  imports: [
    GraphQLRequestModule.forRoot({
      // Exposes configuration options based on the graphql-request package
      endpoint: config.get('endpoint'),
      options: {
        headers: {
          'content-type': 'application/json',
          'x-hasura-admin-secret': config.get('secret'),
        },
      },
    }),
  ],
  providers: [
    {
      // you can provide whatever key you want. use it in conjunction with @Inject("TypeSafeGqlSdk") to get the SDK instance in your controllers/services
      provide: 'TypeSafeGqlSdk',
      inject: [GraphQLClientInject],
      useFactory: (client: GraphQLClient) => getSdk(client),
    },
  ],
})
export class AppModule {
  // ...
}

版本与发布记录

@golevelup/nestjs-graphql-request 共发布 28 个版本,首次发布于 2020/03/28;过去 12 个月发布了 5 个版本。

版本发布日期距今
5.0.0latest2026/03/197 个月前
4.1.12025/12/1910 个月前
4.1.02025/12/1910 个月前
4.0.02025/12/1910 个月前
3.0.02025/12/1910 个月前
0.2.52025/02/191.7 年前

关于 @golevelup/nestjs-graphql-request 的常见问题

@golevelup/nestjs-graphql-request 是什么?

Badass utilities for integrating graphql-request and NestJS

如何安装 @golevelup/nestjs-graphql-request?

在项目目录运行 npm install @golevelup/nestjs-graphql-request,或使用 pnpm add / yarn add / bun add。

@golevelup/nestjs-graphql-request 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@golevelup/nestjs-graphql-request 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@golevelup/nestjs-graphql-request 可以商用吗?

@golevelup/nestjs-graphql-request 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@golevelup/nestjs-graphql-request 的下载量有多少?

根据 npm 官方下载统计,@golevelup/nestjs-graphql-request 在 2026/10/17 – 2026/11/15 这 30 天内被下载 87,356 次,日均约 2,912 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@golevelup/nestjs-graphql-request 还在维护吗?

最新版本 5.0.0 发布于 2026/03/19(7 个月前);至今共发布 28 个版本,其中过去 12 个月发布了 5 个;首次发布于 2020/03/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。