Skip to content

Commit 631ddba

Browse files
authored
Adopt @graphql-yoga/nestjs driver code locally (#3330)
1 parent 3c230ae commit 631ddba

File tree

5 files changed

+58
-36
lines changed

5 files changed

+58
-36
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"@golevelup/nestjs-discovery": "^4.0.0",
4242
"@graphql-hive/yoga": "^0.38.2",
4343
"@graphql-tools/utils": "^10.5.4",
44-
"@graphql-yoga/nestjs": "^3.7.0",
4544
"@graphql-yoga/plugin-apq": "^3.7.0",
4645
"@leeoniya/ufuzzy": "^1.0.11",
4746
"@nestjs/common": "^10.2.7",

src/core/graphql/driver.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11
import { DiscoveryService } from '@golevelup/nestjs-discovery';
2-
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
32
import { Injectable } from '@nestjs/common';
4-
import { HttpAdapter } from '../http';
3+
import {
4+
AbstractGraphQLDriver as AbstractDriver,
5+
GqlModuleOptions,
6+
} from '@nestjs/graphql';
7+
import type { RouteOptions as FastifyRoute } from 'fastify';
8+
import {
9+
createYoga,
10+
YogaServerInstance,
11+
YogaServerOptions,
12+
} from 'graphql-yoga';
13+
import { GqlContextType } from '~/common';
14+
import { HttpAdapter, IRequest } from '../http';
15+
import { IResponse } from '../http/types';
516
import { Plugin } from './plugin.decorator';
617

18+
export interface ServerContext {
19+
/** Cannot be `request` as {@link import('graphql-yoga').YogaInitialContext.request} overrides it */
20+
req: IRequest;
21+
response: IResponse;
22+
}
23+
24+
export type DriverConfig = GqlModuleOptions &
25+
Omit<YogaServerOptions<ServerContext, GqlContextType>, 'context' | 'schema'>;
26+
727
@Injectable()
8-
export class Driver extends YogaDriver<'fastify'> {
28+
export class Driver extends AbstractDriver<DriverConfig> {
29+
private yoga: YogaServerInstance<ServerContext, {}>;
30+
931
constructor(
1032
private readonly discovery: DiscoveryService,
1133
private readonly http: HttpAdapter,
1234
) {
1335
super();
1436
}
1537

16-
async start(options: YogaDriverConfig<'fastify'>) {
38+
async start(options: DriverConfig) {
39+
const fastify = this.http.getInstance();
40+
1741
// Do our plugin discovery / registration
1842
const discoveredPlugins = await this.discovery.providersWithMetaAtKey(
1943
Plugin.KEY,
@@ -23,12 +47,36 @@ export class Driver extends YogaDriver<'fastify'> {
2347
...new Set(discoveredPlugins.map((cls) => cls.discoveredClass.instance)),
2448
];
2549

26-
await super.start(options);
50+
this.yoga = createYoga({
51+
...options,
52+
graphqlEndpoint: options.path,
53+
logging: false,
54+
});
55+
56+
fastify.route({
57+
method: ['GET', 'POST', 'OPTIONS'],
58+
url: this.yoga.graphqlEndpoint,
59+
handler: this.httpHandler,
60+
});
2761

2862
// Setup file upload handling
29-
const fastify = this.http.getInstance();
3063
fastify.addContentTypeParser('multipart/form-data', (req, payload, done) =>
3164
done(null),
3265
);
3366
}
67+
68+
httpHandler: FastifyRoute['handler'] = async (req, reply) => {
69+
const res = await this.yoga.handleNodeRequestAndResponse(req, reply, {
70+
req,
71+
response: reply,
72+
});
73+
return await reply
74+
.headers(Object.fromEntries(res.headers))
75+
.status(res.status)
76+
.send(res.body);
77+
};
78+
79+
async stop() {
80+
// noop
81+
}
3482
}

src/core/graphql/graphql.options.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import { useHive } from '@graphql-hive/yoga';
2-
import {
3-
YogaDriverConfig as DriverConfig,
4-
YogaDriverServerContext,
5-
} from '@graphql-yoga/nestjs';
62
import { useAPQ } from '@graphql-yoga/plugin-apq';
73
import { Injectable } from '@nestjs/common';
84
import { GqlOptionsFactory } from '@nestjs/graphql';
@@ -20,11 +16,11 @@ import { getRegisteredScalars } from '~/common/scalars';
2016
import { ConfigService } from '../config/config.service';
2117
import { VersionService } from '../config/version.service';
2218
import { apolloExplorer } from './apollo-explorer';
19+
import { DriverConfig, ServerContext } from './driver';
2320
import { isGqlContext } from './gql-context.host';
2421
import { GraphqlTracingPlugin } from './graphql-tracing.plugin';
2522

2623
type Plugin = PluginNoContext<GqlContextType>;
27-
type ServerContext = YogaDriverServerContext<'fastify'>;
2824

2925
@Injectable()
3026
export class GraphqlOptions implements GqlOptionsFactory {
@@ -96,14 +92,10 @@ export class GraphqlOptions implements GqlOptionsFactory {
9692
};
9793
}
9894

99-
context = ({
100-
req: request,
101-
reply: response,
102-
}: ServerContext): Partial<GqlContextType> => {
95+
context = ({ req: request }: ServerContext): Partial<GqlContextType> => {
10396
return {
10497
[isGqlContext.KEY]: true,
10598
request,
106-
response,
10799
session$: new BehaviorSubject<Session | undefined>(undefined),
108100
};
109101
};
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { YogaDriverServerContext } from '@graphql-yoga/nestjs';
21
import { createMetadataDecorator } from '@seedcompany/nest';
32
import { Plugin as PluginNoContext } from 'graphql-yoga';
43
import { GqlContextType } from '~/common';
4+
import { ServerContext } from './driver';
55

66
export const Plugin = createMetadataDecorator({
77
types: ['class'],
88
});
99

10-
export type Plugin = PluginNoContext<
11-
GqlContextType,
12-
YogaDriverServerContext<'fastify'>
13-
>;
10+
export type Plugin = PluginNoContext<GqlContextType, ServerContext>;

yarn.lock

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,19 +1958,6 @@ __metadata:
19581958
languageName: node
19591959
linkType: hard
19601960

1961-
"@graphql-yoga/nestjs@npm:^3.7.0":
1962-
version: 3.7.0
1963-
resolution: "@graphql-yoga/nestjs@npm:3.7.0"
1964-
peerDependencies:
1965-
"@nestjs/common": ^10.0.0
1966-
"@nestjs/core": ^10.0.0
1967-
"@nestjs/graphql": ^12.0.0
1968-
graphql: ^15.0.0 || ^16.0.0
1969-
graphql-yoga: ^5.7.0
1970-
checksum: 10c0/459f524504c276b2ddab6759d3d10ef1762c44f7907ff71c0f930d26fb9076441267ac4f764e4131ed6d08068d02a372990339259c0af49a8400e3ede29318f6
1971-
languageName: node
1972-
linkType: hard
1973-
19741961
"@graphql-yoga/plugin-apq@npm:^3.7.0":
19751962
version: 3.7.0
19761963
resolution: "@graphql-yoga/plugin-apq@npm:3.7.0"
@@ -5663,7 +5650,6 @@ __metadata:
56635650
"@graphql-hive/cli": "npm:^0.44.2"
56645651
"@graphql-hive/yoga": "npm:^0.38.2"
56655652
"@graphql-tools/utils": "npm:^10.5.4"
5666-
"@graphql-yoga/nestjs": "npm:^3.7.0"
56675653
"@graphql-yoga/plugin-apq": "npm:^3.7.0"
56685654
"@leeoniya/ufuzzy": "npm:^1.0.11"
56695655
"@nestjs/cli": "npm:^10.2.1"

0 commit comments

Comments
 (0)