-
-
Notifications
You must be signed in to change notification settings - Fork 244
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
Labels
Comments
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));
}
} |
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 |
Thanks, @wolffparkinson! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 aREQUEST
scope via factory provider configuration?The text was updated successfully, but these errors were encountered: