@@ -33,6 +33,7 @@ import { renderGraphiQL } from './renderGraphiQL';
33
33
34
34
type $Request = IncomingMessage ;
35
35
type $Response = ServerResponse & { | json ?: ( data : mixed ) => void | } ;
36
+ type MaybePromise < T > = Promise < T > | T ;
36
37
37
38
/**
38
39
* Used to configure the graphqlHTTP middleware by providing a schema
@@ -46,9 +47,8 @@ export type Options =
46
47
request : $Request ,
47
48
response : $Response ,
48
49
params ? : GraphQLParams ,
49
- ) => OptionsResult )
50
- | OptionsResult ;
51
- export type OptionsResult = OptionsData | Promise < OptionsData > ;
50
+ ) => MaybePromise < OptionsData > )
51
+ | MaybePromise < OptionsData > ;
52
52
53
53
export type OptionsData = { |
54
54
/**
@@ -91,9 +91,7 @@ export type OptionsData = {|
91
91
* An optional function which will be used to execute instead of default `execute`
92
92
* from `graphql-js`.
93
93
*/
94
- customExecuteFn ?: (
95
- args : ExecutionArgs ,
96
- ) => ExecutionResult | Promise < ExecutionResult> ,
94
+ customExecuteFn ?: ( args : ExecutionArgs ) => MaybePromise < ExecutionResult > ,
97
95
98
96
/**
99
97
* An optional function which will be used to format any errors produced by
@@ -126,7 +124,7 @@ export type OptionsData = {|
126
124
*/
127
125
extensions ?: (
128
126
info : RequestInfo ,
129
- ) => { [ key : string ] : mixed , ... } | Promise < { [ key : string ] : mixed , ... } > ,
127
+ ) => MaybePromise < void | { [ key : string ] : mixed , ... } > ,
130
128
131
129
/**
132
130
* A boolean to optionally enable GraphiQL mode.
@@ -347,16 +345,16 @@ export function graphqlHTTP(options: Options): Middleware {
347
345
// Collect and apply any metadata extensions if a function was provided.
348
346
// https://graphql.github.io/graphql-spec/#sec-Response-Format
349
347
if ( extensionsFn ) {
350
- const extensionsObj = await extensionsFn ( {
348
+ const extensions = await extensionsFn ( {
351
349
document : documentAST ,
352
350
variables,
353
351
operationName,
354
352
result,
355
353
context,
356
354
} ) ;
357
355
358
- if ( extensionsObj != null && typeof extensionsObj === 'object' ) {
359
- ( result : any ) . extensions = extensionsObj ;
356
+ if ( extensions != null ) {
357
+ result = { ... result , extensions } ;
360
358
}
361
359
}
362
360
} catch ( error ) {
0 commit comments