@@ -106,30 +106,11 @@ export abstract class AbstractYogaDriver<
106106 }
107107 }
108108
109+ const schema = this . mergeConditionalSchema < 'express' > ( conditionalSchema , options . schema ) ;
110+
109111 const yoga = createYoga < YogaDriverServerContext < 'express' > > ( {
110112 ...options ,
111- schema : async request => {
112- const schemas : GraphQLSchema [ ] = [ ] ;
113-
114- if ( options . schema ) {
115- schemas . push ( options . schema ) ;
116- }
117-
118- if ( conditionalSchema ) {
119- const conditionalSchemaResult =
120- typeof conditionalSchema === 'function'
121- ? await conditionalSchema ( request )
122- : await conditionalSchema ;
123-
124- if ( conditionalSchemaResult ) {
125- schemas . push ( conditionalSchemaResult ) ;
126- }
127- }
128-
129- return mergeSchemas ( {
130- schemas,
131- } ) ;
132- } ,
113+ schema,
133114 graphqlEndpoint : options . path ,
134115 // disable logging by default
135116 // however, if `true` use nest logger
@@ -154,30 +135,11 @@ export abstract class AbstractYogaDriver<
154135
155136 preStartHook ?.( app ) ;
156137
138+ const schema = this . mergeConditionalSchema < 'fastify' > ( conditionalSchema , options . schema ) ;
139+
157140 const yoga = createYoga < YogaDriverServerContext < 'fastify' > > ( {
158141 ...options ,
159- schema : async request => {
160- const schemas : GraphQLSchema [ ] = [ ] ;
161-
162- if ( options . schema ) {
163- schemas . push ( options . schema ) ;
164- }
165-
166- if ( conditionalSchema ) {
167- const conditionalSchemaResult =
168- typeof conditionalSchema === 'function'
169- ? await conditionalSchema ( request )
170- : await conditionalSchema ;
171-
172- if ( conditionalSchemaResult ) {
173- schemas . push ( conditionalSchemaResult ) ;
174- }
175- }
176-
177- return mergeSchemas ( {
178- schemas,
179- } ) ;
180- } ,
142+ schema,
181143 graphqlEndpoint : options . path ,
182144 // disable logging by default
183145 // however, if `true` use fastify logger
@@ -198,6 +160,38 @@ export abstract class AbstractYogaDriver<
198160 } ) ;
199161 }
200162
163+ private mergeConditionalSchema < T extends YogaDriverPlatform > (
164+ conditionalSchema : YogaSchemaDefinition < YogaDriverServerContext < T > , never > | undefined ,
165+ schema ?: GraphQLSchema ,
166+ ) {
167+ let mergedSchema : YogaSchemaDefinition < YogaDriverServerContext < T > , never > | undefined = schema ;
168+
169+ if ( conditionalSchema ) {
170+ mergedSchema = async request => {
171+ const schemas : GraphQLSchema [ ] = [ ] ;
172+
173+ if ( schema ) {
174+ schemas . push ( schema ) ;
175+ }
176+
177+ const conditionalSchemaResult =
178+ typeof conditionalSchema === 'function'
179+ ? await conditionalSchema ( request )
180+ : await conditionalSchema ;
181+
182+ if ( conditionalSchemaResult ) {
183+ schemas . push ( conditionalSchemaResult ) ;
184+ }
185+
186+ return mergeSchemas ( {
187+ schemas,
188+ } ) ;
189+ } ;
190+ }
191+
192+ return mergedSchema ;
193+ }
194+
201195 public subscriptionWithFilter < TPayload , TVariables , TContext > (
202196 instanceRef : unknown ,
203197 filterFn : (
0 commit comments