Skip to content

Commit bf9c29f

Browse files
committed
Set up our own Plugin discovery for Yoga/NestJS
1 parent 879950f commit bf9c29f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/core/graphql/driver.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1+
import { DiscoveryService } from '@golevelup/nestjs-discovery';
12
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs';
23
import { Injectable } from '@nestjs/common';
34
import { HttpAdapter } from '../http';
5+
import { Plugin } from './plugin.decorator';
46

57
@Injectable()
68
export class Driver extends YogaDriver<'fastify'> {
7-
constructor(private readonly http: HttpAdapter) {
9+
constructor(
10+
private readonly discovery: DiscoveryService,
11+
private readonly http: HttpAdapter,
12+
) {
813
super();
914
}
1015

1116
async start(options: YogaDriverConfig<'fastify'>) {
17+
// Do our plugin discovery / registration
18+
const discoveredPlugins = await this.discovery.providersWithMetaAtKey(
19+
Plugin.KEY,
20+
);
21+
options.plugins = [
22+
...(options.plugins ?? []),
23+
...discoveredPlugins.map((cls) => cls.discoveredClass.instance),
24+
];
25+
1226
await super.start(options);
1327

1428
// Setup file upload handling

src/core/graphql/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export {
44
isGqlContext,
55
NotGraphQLContext,
66
} from './gql-context.host';
7+
export * from './plugin.decorator';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { YogaDriverServerContext } from '@graphql-yoga/nestjs';
2+
import { createMetadataDecorator } from '@seedcompany/nest';
3+
import { Plugin as PluginNoContext } from 'graphql-yoga';
4+
import { GqlContextType } from '~/common';
5+
6+
export const Plugin = createMetadataDecorator({
7+
types: ['class'],
8+
});
9+
10+
export type Plugin = PluginNoContext<
11+
GqlContextType,
12+
YogaDriverServerContext<'fastify'>
13+
>;

0 commit comments

Comments
 (0)