@@ -114,6 +114,15 @@ export interface OptionsData {
114
114
*/
115
115
formatError ?: ( error : GraphQLError ) => GraphQLFormattedError ;
116
116
117
+ /**
118
+ * Use this to modify the response when a runtime query error occurs. By
119
+ * default the statusCode will be set to 500.
120
+ */
121
+ handleRuntimeQueryErrorFn ?: (
122
+ result : ExecutionResult ,
123
+ response : Response ,
124
+ ) => void ;
125
+
117
126
/**
118
127
* An optional function for adding additional metadata to the GraphQL response
119
128
* as a key-value object. The result will be added to "extensions" field in
@@ -201,6 +210,7 @@ export function graphqlHTTP(options: Options): Middleware {
201
210
let formatErrorFn = formatError ;
202
211
let pretty = false ;
203
212
let result : ExecutionResult ;
213
+ let optionsData : OptionsData | undefined ;
204
214
205
215
try {
206
216
// Parse the Request to get GraphQL request parameters.
@@ -209,7 +219,7 @@ export function graphqlHTTP(options: Options): Middleware {
209
219
} catch ( error ) {
210
220
// When we failed to parse the GraphQL parameters, we still need to get
211
221
// the options object, so make an options call to resolve just that.
212
- const optionsData = await resolveOptions ( ) ;
222
+ optionsData = await resolveOptions ( ) ;
213
223
pretty = optionsData . pretty ?? false ;
214
224
formatErrorFn =
215
225
optionsData . customFormatErrorFn ??
@@ -219,7 +229,7 @@ export function graphqlHTTP(options: Options): Middleware {
219
229
}
220
230
221
231
// Then, resolve the Options to get OptionsData.
222
- const optionsData : OptionsData = await resolveOptions ( params ) ;
232
+ optionsData = await resolveOptions ( params ) ;
223
233
224
234
// Collect information from the options data object.
225
235
const schema = optionsData . schema ;
@@ -373,13 +383,22 @@ export function graphqlHTTP(options: Options): Middleware {
373
383
result = { data : undefined , errors : error . graphqlErrors ?? [ error ] } ;
374
384
}
375
385
376
- // If no data was included in the result, that indicates a runtime query
377
- // error, indicate as such with a generic status code.
378
- // Note: Information about the error itself will still be contained in
379
- // the resulting JSON payload.
380
- // https://graphql.github.io/graphql-spec/#sec-Data
381
- if ( response . statusCode === 200 && result . data == null ) {
382
- response . statusCode = 500 ;
386
+ if ( result . errors != null || result . data == null ) {
387
+ const handleRuntimeQueryErrorFn =
388
+ optionsData ?. handleRuntimeQueryErrorFn ??
389
+ ( ( _result , _response ) => {
390
+ // If no data was included in the result, that indicates a runtime query
391
+ // error, indicate as such with a generic status code.
392
+ // Note: Information about the error itself will still be contained in
393
+ // the resulting JSON payload.
394
+ // https://graphql.github.io/graphql-spec/#sec-Data
395
+
396
+ if ( _response . statusCode === 200 && _result . data == null ) {
397
+ _response . statusCode = 500 ;
398
+ }
399
+ } ) ;
400
+
401
+ handleRuntimeQueryErrorFn ( result , response ) ;
383
402
}
384
403
385
404
// Format any encountered errors.
0 commit comments