Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@golevelup/nestjs-graphql-request

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@golevelup/nestjs-graphql-request

Badass utilities for integrating graphql-request and NestJS

  • 0.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.1K
increased by0.98%
Maintainers
0
Weekly downloads
 
Created
Source

@golevelup/nestjs-graphql-request

version downloads license

Description

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

Usage

Install

npm install ---save @golevelup/nestjs-graphql-request

or

yarn 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(GraphQLRequestModule, {
      // 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(GraphQLRequestModule, {
      // 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 {
  // ...
}

Contribute

Contributions welcome! Read the contribution guidelines first.

License

MIT License

Keywords

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc