1
1
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' ;
7
6
import { Injectable } from '@nestjs/common' ;
8
7
import { GqlOptionsFactory } from '@nestjs/graphql' ;
9
8
import { CacheService } from '@seedcompany/cache' ;
10
9
import { mapKeys } from '@seedcompany/common' ;
11
10
import { GraphQLScalarType , OperationDefinitionNode } from 'graphql' ;
11
+ import { Plugin } from 'graphql-yoga' ;
12
12
import { BehaviorSubject } from 'rxjs' ;
13
13
import { GqlContextType , ServerException , Session } from '~/common' ;
14
14
import { getRegisteredScalars } from '~/common/scalars' ;
@@ -18,6 +18,8 @@ import { isGqlContext } from './gql-context.host';
18
18
import { GraphqlErrorFormatter } from './graphql-error-formatter' ;
19
19
import { GraphqlTracingPlugin } from './graphql-tracing.plugin' ;
20
20
21
+ type ServerContext = YogaDriverServerContext < 'fastify' > ;
22
+
21
23
@Injectable ( )
22
24
export class GraphqlOptions implements GqlOptionsFactory {
23
25
constructor (
@@ -45,50 +47,48 @@ export class GraphqlOptions implements GqlOptionsFactory {
45
47
return {
46
48
path : '/graphql/:opName?' ,
47
49
autoSchemaFile : 'schema.graphql' ,
50
+ graphiql : {
51
+ title : 'CORD API' ,
52
+ defaultEditorToolsVisibility : false ,
53
+ credentials : 'include' ,
54
+ } ,
48
55
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
54
56
sortSchema : true ,
55
57
buildSchemaOptions : {
56
- fieldMiddleware : [ this . tracing . fieldMiddleware ( ) ] ,
58
+ // fieldMiddleware: [this.tracing.fieldMiddleware()],
57
59
} ,
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 ,
65
60
resolvers : {
66
61
...scalars ,
67
62
} ,
68
63
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,
79
66
] ,
80
67
} ;
81
68
}
82
69
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
+ }
92
92
}
93
93
94
94
export const createFakeStubOperation = ( ) => {
0 commit comments