Skip to content

Commit

Permalink
fix(gql): interceptors now work with subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Jan 1, 2021
1 parent 48ec392 commit 55ba385
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
25 changes: 18 additions & 7 deletions integration/src/gql/gql.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
// import { PubSub } from 'graphql-subscriptions';
import { AppService } from '../app.service';
import { GqlResolver } from './gql.resolver';

@Module({
imports: [
GraphQLModule.forRoot({
context: ({ req, res, request, reply }) => ({
req,
res,
request,
reply,
}),
context: async ({ req, res, request, reply }) => {
return {
req,
res,
request,
reply,
};
},
autoSchemaFile: true,
// installSubscriptionHandlers: true,
}),
],
providers: [AppService, GqlResolver],
providers: [
AppService,
GqlResolver,
/* {
provide: 'PUB_SUB',
useClass: PubSub,
}, */
],
})
export class GqlModule {}
17 changes: 14 additions & 3 deletions integration/src/gql/gql.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { BadRequestException } from '@nestjs/common';
import { Mutation, Query, Resolver } from '@nestjs/graphql';
import { Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { OgmaSkip } from '@ogma/nestjs-module';
// import { PubSub } from 'graphql-subscriptions';
import { AppService } from '../app.service';
import { SimpleObject } from './simple-object.model';

@Resolver(() => SimpleObject)
export class GqlResolver {
constructor(private readonly appService: AppService) {}
constructor(
private readonly appService: AppService, // @Inject('PUB_SUB') private readonly pubSub: PubSub,
) {}

@Query(() => SimpleObject)
getQuery(): SimpleObject {
Expand All @@ -26,6 +29,14 @@ export class GqlResolver {
@OgmaSkip()
@Query(() => SimpleObject)
getSkip(): SimpleObject {
return this.appService.getHello();
// eslint-disable-next-line sonarjs/prefer-immediate-return
const obj = this.appService.getHello();
// this.pubSub.publish('subscribed', { subscribed: obj });
return obj;
}

/* @Subscription(() => SimpleObject)
subscribed() {
return this.pubSub.asyncIterator('subscribed');
} */
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ export class GraphQLFastifyParser extends FastifyParser {
getResponse(context: ExecutionContext): FastifyReply {
return this.getContext(context).getContext().reply;
}

setRequestId(context: ExecutionContext, requestId: string): void {
const ctx = this.getContext(context).getContext();
if (ctx.request) {
ctx.request.requestId = requestId;
} else {
ctx.requestId = requestId;
}
}
}
9 changes: 9 additions & 0 deletions packages/platform-graphql/src/graphql-interceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ export class GraphQLParser extends ExpressParser {
getResponse(context: ExecutionContext): Response {
return this.getContext(context).getContext().res;
}

setRequestId(context: ExecutionContext, requestId: string): void {
const ctx = this.getContext(context).getContext();
if (ctx.req) {
ctx.req.requestId = requestId;
} else {
ctx.requestId = requestId;
}
}
}

0 comments on commit 55ba385

Please sign in to comment.