Skip to content

Commit

Permalink
Adopt @graphql-yoga/nestjs driver code locally (#3330)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF authored Nov 11, 2024
1 parent 3c230ae commit 631ddba
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@golevelup/nestjs-discovery": "^4.0.0",
"@graphql-hive/yoga": "^0.38.2",
"@graphql-tools/utils": "^10.5.4",
"@graphql-yoga/nestjs": "^3.7.0",
"@graphql-yoga/plugin-apq": "^3.7.0",
"@leeoniya/ufuzzy": "^1.0.11",
"@nestjs/common": "^10.2.7",
Expand Down
60 changes: 54 additions & 6 deletions src/core/graphql/driver.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
import { DiscoveryService } from '@golevelup/nestjs-discovery';
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
import { Injectable } from '@nestjs/common';
import { HttpAdapter } from '../http';
import {
AbstractGraphQLDriver as AbstractDriver,
GqlModuleOptions,
} from '@nestjs/graphql';
import type { RouteOptions as FastifyRoute } from 'fastify';
import {
createYoga,
YogaServerInstance,
YogaServerOptions,
} from 'graphql-yoga';
import { GqlContextType } from '~/common';
import { HttpAdapter, IRequest } from '../http';
import { IResponse } from '../http/types';
import { Plugin } from './plugin.decorator';

export interface ServerContext {
/** Cannot be `request` as {@link import('graphql-yoga').YogaInitialContext.request} overrides it */
req: IRequest;
response: IResponse;
}

export type DriverConfig = GqlModuleOptions &
Omit<YogaServerOptions<ServerContext, GqlContextType>, 'context' | 'schema'>;

@Injectable()
export class Driver extends YogaDriver<'fastify'> {
export class Driver extends AbstractDriver<DriverConfig> {
private yoga: YogaServerInstance<ServerContext, {}>;

constructor(
private readonly discovery: DiscoveryService,
private readonly http: HttpAdapter,
) {
super();
}

async start(options: YogaDriverConfig<'fastify'>) {
async start(options: DriverConfig) {
const fastify = this.http.getInstance();

// Do our plugin discovery / registration
const discoveredPlugins = await this.discovery.providersWithMetaAtKey(
Plugin.KEY,
Expand All @@ -23,12 +47,36 @@ export class Driver extends YogaDriver<'fastify'> {
...new Set(discoveredPlugins.map((cls) => cls.discoveredClass.instance)),
];

await super.start(options);
this.yoga = createYoga({
...options,
graphqlEndpoint: options.path,
logging: false,
});

fastify.route({
method: ['GET', 'POST', 'OPTIONS'],
url: this.yoga.graphqlEndpoint,
handler: this.httpHandler,
});

// Setup file upload handling
const fastify = this.http.getInstance();
fastify.addContentTypeParser('multipart/form-data', (req, payload, done) =>
done(null),
);
}

httpHandler: FastifyRoute['handler'] = async (req, reply) => {
const res = await this.yoga.handleNodeRequestAndResponse(req, reply, {
req,
response: reply,
});
return await reply
.headers(Object.fromEntries(res.headers))
.status(res.status)
.send(res.body);
};

async stop() {
// noop
}
}
12 changes: 2 additions & 10 deletions src/core/graphql/graphql.options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { useHive } from '@graphql-hive/yoga';
import {
YogaDriverConfig as DriverConfig,
YogaDriverServerContext,
} from '@graphql-yoga/nestjs';
import { useAPQ } from '@graphql-yoga/plugin-apq';
import { Injectable } from '@nestjs/common';
import { GqlOptionsFactory } from '@nestjs/graphql';
Expand All @@ -20,11 +16,11 @@ import { getRegisteredScalars } from '~/common/scalars';
import { ConfigService } from '../config/config.service';
import { VersionService } from '../config/version.service';
import { apolloExplorer } from './apollo-explorer';
import { DriverConfig, ServerContext } from './driver';
import { isGqlContext } from './gql-context.host';
import { GraphqlTracingPlugin } from './graphql-tracing.plugin';

type Plugin = PluginNoContext<GqlContextType>;
type ServerContext = YogaDriverServerContext<'fastify'>;

@Injectable()
export class GraphqlOptions implements GqlOptionsFactory {
Expand Down Expand Up @@ -96,14 +92,10 @@ export class GraphqlOptions implements GqlOptionsFactory {
};
}

context = ({
req: request,
reply: response,
}: ServerContext): Partial<GqlContextType> => {
context = ({ req: request }: ServerContext): Partial<GqlContextType> => {
return {
[isGqlContext.KEY]: true,
request,
response,
session$: new BehaviorSubject<Session | undefined>(undefined),
};
};
Expand Down
7 changes: 2 additions & 5 deletions src/core/graphql/plugin.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { YogaDriverServerContext } from '@graphql-yoga/nestjs';
import { createMetadataDecorator } from '@seedcompany/nest';
import { Plugin as PluginNoContext } from 'graphql-yoga';
import { GqlContextType } from '~/common';
import { ServerContext } from './driver';

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

export type Plugin = PluginNoContext<
GqlContextType,
YogaDriverServerContext<'fastify'>
>;
export type Plugin = PluginNoContext<GqlContextType, ServerContext>;
14 changes: 0 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1958,19 +1958,6 @@ __metadata:
languageName: node
linkType: hard

"@graphql-yoga/nestjs@npm:^3.7.0":
version: 3.7.0
resolution: "@graphql-yoga/nestjs@npm:3.7.0"
peerDependencies:
"@nestjs/common": ^10.0.0
"@nestjs/core": ^10.0.0
"@nestjs/graphql": ^12.0.0
graphql: ^15.0.0 || ^16.0.0
graphql-yoga: ^5.7.0
checksum: 10c0/459f524504c276b2ddab6759d3d10ef1762c44f7907ff71c0f930d26fb9076441267ac4f764e4131ed6d08068d02a372990339259c0af49a8400e3ede29318f6
languageName: node
linkType: hard

"@graphql-yoga/plugin-apq@npm:^3.7.0":
version: 3.7.0
resolution: "@graphql-yoga/plugin-apq@npm:3.7.0"
Expand Down Expand Up @@ -5663,7 +5650,6 @@ __metadata:
"@graphql-hive/cli": "npm:^0.44.2"
"@graphql-hive/yoga": "npm:^0.38.2"
"@graphql-tools/utils": "npm:^10.5.4"
"@graphql-yoga/nestjs": "npm:^3.7.0"
"@graphql-yoga/plugin-apq": "npm:^3.7.0"
"@leeoniya/ufuzzy": "npm:^1.0.11"
"@nestjs/cli": "npm:^10.2.1"
Expand Down

0 comments on commit 631ddba

Please sign in to comment.