Skip to content

Commit 084e820

Browse files
committed
Apollo -> Yoga
1 parent 7513391 commit 084e820

File tree

4 files changed

+268
-66
lines changed

4 files changed

+268
-66
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
"@fastify/cors": "^9.0.1",
4343
"@ffprobe-installer/ffprobe": "^2.1.2",
4444
"@golevelup/nestjs-discovery": "^4.0.0",
45+
"@graphql-tools/utils": "^10.5.4",
46+
"@graphql-yoga/nestjs": "^3.7.0",
47+
"@graphql-yoga/plugin-apq": "^3.7.0",
4548
"@leeoniya/ufuzzy": "^1.0.11",
4649
"@nestjs/apollo": "^12.0.9",
4750
"@nestjs/common": "^10.2.7",
@@ -76,10 +79,11 @@
7679
"file-type": "^18.6.0",
7780
"glob": "^10.3.10",
7881
"got": "^14.3.0",
79-
"graphql": "^16.8.1",
82+
"graphql": "^16.9.0",
8083
"graphql-parse-resolve-info": "^4.14.0",
8184
"graphql-scalars": "^1.22.4",
8285
"graphql-upload": "^16.0.2",
86+
"graphql-yoga": "^5.7.0",
8387
"human-format": "^1.2.0",
8488
"image-size": "^1.0.2",
8589
"indent-string": "^5.0.0",

src/core/graphql/graphql.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApolloDriver } from '@nestjs/apollo';
1+
import { YogaDriver } from '@graphql-yoga/nestjs';
22
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
33
import { APP_INTERCEPTOR } from '@nestjs/core';
44
import { GraphQLModule as NestGraphqlModule } from '@nestjs/graphql';
@@ -34,7 +34,7 @@ export class GraphqlOptionsModule {}
3434
@Module({
3535
imports: [
3636
NestGraphqlModule.forRootAsync({
37-
driver: ApolloDriver,
37+
driver: YogaDriver,
3838
useExisting: GraphqlOptions,
3939
imports: [GraphqlOptionsModule],
4040
}),

src/core/graphql/graphql.options.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {
2-
ApolloServerPluginLandingPageLocalDefault,
3-
ApolloServerPluginLandingPageProductionDefault,
4-
} from '@apollo/server/plugin/landingPage/default';
5-
import { ApolloFastifyContextFunctionArgument } from '@as-integrations/fastify';
6-
import { ApolloDriverConfig as DriverConfig } from '@nestjs/apollo';
2+
YogaDriverConfig as DriverConfig,
3+
YogaDriverServerContext,
4+
} from '@graphql-yoga/nestjs';
5+
import { useAPQ } from '@graphql-yoga/plugin-apq';
76
import { Injectable } from '@nestjs/common';
87
import { GqlOptionsFactory } from '@nestjs/graphql';
98
import { CacheService } from '@seedcompany/cache';
109
import { mapKeys } from '@seedcompany/common';
1110
import { GraphQLScalarType, OperationDefinitionNode } from 'graphql';
11+
import { Plugin } from 'graphql-yoga';
1212
import { BehaviorSubject } from 'rxjs';
1313
import { GqlContextType, ServerException, Session } from '~/common';
1414
import { getRegisteredScalars } from '~/common/scalars';
@@ -18,6 +18,8 @@ import { isGqlContext } from './gql-context.host';
1818
import { GraphqlErrorFormatter } from './graphql-error-formatter';
1919
import { GraphqlTracingPlugin } from './graphql-tracing.plugin';
2020

21+
type ServerContext = YogaDriverServerContext<'fastify'>;
22+
2123
@Injectable()
2224
export class GraphqlOptions implements GqlOptionsFactory {
2325
constructor(
@@ -45,50 +47,48 @@ export class GraphqlOptions implements GqlOptionsFactory {
4547
return {
4648
path: '/graphql/:opName?',
4749
autoSchemaFile: 'schema.graphql',
50+
graphiql: {
51+
title: 'CORD API',
52+
defaultEditorToolsVisibility: false,
53+
credentials: 'include',
54+
},
4855
context: this.context,
49-
playground: false,
50-
introspection: true,
51-
formatError: this.errorFormatter.formatError,
52-
includeStacktraceInErrorResponses: true,
53-
status400ForVariableCoercionErrors: true, // will be default in v5
5456
sortSchema: true,
5557
buildSchemaOptions: {
56-
fieldMiddleware: [this.tracing.fieldMiddleware()],
58+
// fieldMiddleware: [this.tracing.fieldMiddleware()],
5759
},
58-
cache: this.cache.adaptTo.apollo({
59-
ttl: this.config.graphQL.persistedQueries.ttl,
60-
refreshTtlOnGet: true,
61-
}),
62-
persistedQueries: this.config.graphQL.persistedQueries.enabled
63-
? {}
64-
: false,
6560
resolvers: {
6661
...scalars,
6762
},
6863
plugins: [
69-
process.env.APOLLO_GRAPH_REF
70-
? ApolloServerPluginLandingPageProductionDefault({
71-
graphRef: process.env.APOLLO_GRAPH_REF,
72-
embed: true,
73-
includeCookies: true,
74-
})
75-
: ApolloServerPluginLandingPageLocalDefault({
76-
embed: true,
77-
includeCookies: true,
78-
}),
64+
this.useAutomaticPersistedQueries(),
65+
// more,
7966
],
8067
};
8168
}
8269

83-
context = (
84-
...[request, response]: ApolloFastifyContextFunctionArgument
85-
): GqlContextType => ({
86-
[isGqlContext.KEY]: true,
87-
request,
88-
response,
89-
operation: createFakeStubOperation(),
90-
session$: new BehaviorSubject<Session | undefined>(undefined),
91-
});
70+
context = ({
71+
req: request,
72+
reply: response,
73+
}: ServerContext): GqlContextType => {
74+
return {
75+
[isGqlContext.KEY]: true,
76+
request,
77+
response,
78+
operation: createFakeStubOperation(),
79+
session$: new BehaviorSubject<Session | undefined>(undefined),
80+
};
81+
};
82+
83+
private useAutomaticPersistedQueries(): Plugin | false {
84+
const { enabled, ttl } = this.config.graphQL.persistedQueries;
85+
if (!enabled) {
86+
return false;
87+
}
88+
89+
const store = this.cache.namespace('apq:', { ttl, refreshTtlOnGet: true });
90+
return useAPQ({ store });
91+
}
9292
}
9393

9494
export const createFakeStubOperation = () => {

0 commit comments

Comments
 (0)