Skip to content

Commit 1d16b94

Browse files
committed
Convert _adding operation to context_ to yoga plugin
1 parent a0ea2e6 commit 1d16b94

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/core/graphql/gql-context.host.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ export class GqlContextHostImpl
103103
return {};
104104
}
105105
store.ctx = context;
106-
return {
107-
async didResolveOperation({ operation }) {
108-
context.operation = operation!;
109-
},
110-
};
106+
return {};
111107
}
112108

113109
onModuleDestroy() {

src/core/graphql/graphql.options.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ import { Injectable } from '@nestjs/common';
77
import { GqlOptionsFactory } from '@nestjs/graphql';
88
import { CacheService } from '@seedcompany/cache';
99
import { mapKeys } from '@seedcompany/common';
10-
import { GraphQLScalarType, OperationDefinitionNode } from 'graphql';
11-
import { Plugin } from 'graphql-yoga';
10+
import {
11+
DocumentNode,
12+
GraphQLScalarType,
13+
OperationDefinitionNode,
14+
} from 'graphql';
15+
import { Plugin as PluginNoContext } from 'graphql-yoga';
1216
import { BehaviorSubject } from 'rxjs';
13-
import { GqlContextType, ServerException, Session } from '~/common';
17+
import { GqlContextType, Session } from '~/common';
1418
import { getRegisteredScalars } from '~/common/scalars';
1519
import { ConfigService } from '../config/config.service';
1620
import { VersionService } from '../config/version.service';
1721
import { isGqlContext } from './gql-context.host';
1822
import { GraphqlTracingPlugin } from './graphql-tracing.plugin';
1923

24+
type Plugin = PluginNoContext<GqlContextType>;
2025
type ServerContext = YogaDriverServerContext<'fastify'>;
2126

2227
@Injectable()
@@ -54,14 +59,14 @@ export class GraphqlOptions implements GqlOptionsFactory {
5459
maskedErrors: false, // Errors are formatted in plugin
5560
sortSchema: true,
5661
buildSchemaOptions: {
57-
// fieldMiddleware: [this.tracing.fieldMiddleware()],
62+
fieldMiddleware: [this.tracing.fieldMiddleware()],
5863
},
5964
resolvers: {
6065
...scalars,
6166
},
6267
plugins: [
6368
this.useAutomaticPersistedQueries(),
64-
// more,
69+
this.useAddOperationToContext(),
6570
],
6671
fetchAPI: {
6772
// @whatwg-node/node-fetch polyfill doesn't keep square brackets for ipv6 hostname
@@ -75,17 +80,16 @@ export class GraphqlOptions implements GqlOptionsFactory {
7580
context = ({
7681
req: request,
7782
reply: response,
78-
}: ServerContext): GqlContextType => {
83+
}: ServerContext): Partial<GqlContextType> => {
7984
return {
8085
[isGqlContext.KEY]: true,
8186
request,
8287
response,
83-
operation: createFakeStubOperation(),
8488
session$: new BehaviorSubject<Session | undefined>(undefined),
8589
};
8690
};
8791

88-
private useAutomaticPersistedQueries(): Plugin | false {
92+
private useAutomaticPersistedQueries(): PluginNoContext | false {
8993
const { enabled, ttl } = this.config.graphQL.persistedQueries;
9094
if (!enabled) {
9195
return false;
@@ -94,13 +98,16 @@ export class GraphqlOptions implements GqlOptionsFactory {
9498
const store = this.cache.namespace('apq:', { ttl, refreshTtlOnGet: true });
9599
return useAPQ({ store });
96100
}
97-
}
98101

99-
export const createFakeStubOperation = () => {
100-
const operation = {} as unknown as OperationDefinitionNode;
101-
return new Proxy(operation, {
102-
get() {
103-
throw new ServerException('GQL operation has not been determined yet');
104-
},
105-
});
106-
};
102+
private useAddOperationToContext(): Plugin {
103+
return {
104+
onValidate: ({ params, extendContext }) => {
105+
const document: DocumentNode = params.documentAST;
106+
const operation = document.definitions.find(
107+
(d): d is OperationDefinitionNode => d.kind === 'OperationDefinition',
108+
)!;
109+
extendContext({ operation });
110+
},
111+
};
112+
}
113+
}

0 commit comments

Comments
 (0)