Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQLClient: Request provider scope #641

Closed
namoscato opened this issue Aug 28, 2023 · 3 comments
Closed

GraphQLClient: Request provider scope #641

namoscato opened this issue Aug 28, 2023 · 3 comments
Labels

Comments

@namoscato
Copy link

We have a scenario in which a NestJS request header needs to inform graphql-request client instantiation (i.e. affecting GraphQL client request headers).

Is there a way to achieve this behavior via GraphQLRequestModule? More generally, do you know if there is a way to configure a REQUEST scope via factory provider configuration?

@wolffparkinson
Copy link

wolffparkinson commented Oct 3, 2023

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));
  }
}

@underfisk
Copy link
Contributor

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

@namoscato
Copy link
Author

Thanks, @wolffparkinson!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants