Giter Site home page Giter Site logo

Comments (3)

wolffparkinson avatar wolffparkinson commented on June 19, 2024 1

Here is a solution that worked for me with respect to client instantiation with request scoped headers

// gql-client.module.ts

import { GraphQLRequestModule } from '@golevelup/nestjs-graphql-request';
import { Global, Module } from '@nestjs/common';
import { moduleConfig } from '~/config';

@Global()
@Module({
  imports: [
    GraphQLRequestModule.forRoot(GraphQLRequestModule, moduleConfig.gqlClient),
  ],
  providers: [],
  exports: [GraphQLRequestModule],
})
export class GqlClientModule {}
// gql-client.decorator.ts

import { InjectGraphQLClient } from '@golevelup/nestjs-graphql-request';
import { Injectable, PipeTransform, createParamDecorator } from '@nestjs/common';
import { GraphQLClient } from 'graphql-request';
import { getSdk, type Sdk } from './generated';

export type IGql = Sdk;

@Injectable()
export class GqlClientPipe implements PipeTransform<ExecutionContext, Sdk> {
  constructor(@InjectGraphQLClient() private readonly client: GraphQLClient) {}

  transform(ctx: ExecutionContext) {
       
        // Your logic to build headers
       const headers = {
         'X-SOURCE': createHmac(....)
    };

    return getSdk(this.client, (action) => action(headers));
  }
}

export const Gql = () => createParamDecorator((data,ctx)=>ctx)(GqlClientPipe);

Example usage

// hi-command.ts

@Injectable()
export class HiCommand {
  @TextCommand({ name: 'hi'})
  async onHi(@Reply() reply: EventReply, @Gql() gql: IGql) {
    const { me } = await gql.Me();
    return reply.text(JSON.stringify(me));
  }
}

from nestjs.

namoscato avatar namoscato commented on June 19, 2024 1

Thanks, @wolffparkinson!

from nestjs.

underfisk avatar underfisk commented on June 19, 2024

It looks like @wolffparkinson solution should work enough for your use case but if you feel that you're able to have a PR with this feature implemented, I'll be happy to review but meanwhile, I'm marking this as closed

from nestjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.