Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 00cf8d0

Browse files
committed
Allow extensions callback to return undefined
1 parent ebba4a9 commit 00cf8d0

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

src/__tests__/http-test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,13 +2251,10 @@ function runTests(server: Server) {
22512251

22522252
app.get(
22532253
urlString(),
2254-
// @ts-expect-error
22552254
graphqlHTTP(() => ({
22562255
schema: TestSchema,
22572256
context: { foo: 'bar' },
2258-
extensions({ context }) {
2259-
return () => ({ contextValue: JSON.stringify(context) });
2260-
},
2257+
extensions: () => undefined,
22612258
})),
22622259
);
22632260

src/index.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {};
2323
type Request = IncomingMessage;
2424

2525
type Response = ServerResponse & { json?: (data: unknown) => void };
26+
type MaybePromise<T> = Promise<T> | T;
2627

2728
type Middleware = (request: Request, response: Response) => Promise<void>;
2829

@@ -38,9 +39,8 @@ export type Options =
3839
request: Request,
3940
response: Response,
4041
params?: GraphQLParams,
41-
) => OptionsResult)
42-
| OptionsResult;
43-
type OptionsResult = OptionsData | Promise<OptionsData>;
42+
) => MaybePromise<OptionsData>)
43+
| MaybePromise<OptionsData>;
4444

4545
export interface OptionsData {
4646
/**
@@ -83,9 +83,7 @@ export interface OptionsData {
8383
* An optional function which will be used to execute instead of default `execute`
8484
* from `graphql-js`.
8585
*/
86-
customExecuteFn?: (
87-
args: ExecutionArgs,
88-
) => ExecutionResult | Promise<ExecutionResult>;
86+
customExecuteFn?: (args: ExecutionArgs) => MaybePromise<ExecutionResult>;
8987

9088
/**
9189
* An optional function which will be used to format any errors produced by
@@ -118,7 +116,7 @@ export interface OptionsData {
118116
*/
119117
extensions?: (
120118
info: RequestInfo,
121-
) => { [key: string]: unknown } | Promise<{ [key: string]: unknown }>;
119+
) => MaybePromise<undefined | { [key: string]: unknown }>;
122120

123121
/**
124122
* A boolean to optionally enable GraphiQL mode.

src/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { renderGraphiQL } from './renderGraphiQL';
3333

3434
type $Request = IncomingMessage;
3535
type $Response = ServerResponse & {| json?: (data: mixed) => void |};
36+
type MaybePromise<T> = Promise<T> | T;
3637

3738
/**
3839
* Used to configure the graphqlHTTP middleware by providing a schema
@@ -46,9 +47,8 @@ export type Options =
4647
request: $Request,
4748
response: $Response,
4849
params?: GraphQLParams,
49-
) => OptionsResult)
50-
| OptionsResult;
51-
export type OptionsResult = OptionsData | Promise<OptionsData>;
50+
) => MaybePromise<OptionsData>)
51+
| MaybePromise<OptionsData>;
5252

5353
export type OptionsData = {|
5454
/**
@@ -91,9 +91,7 @@ export type OptionsData = {|
9191
* An optional function which will be used to execute instead of default `execute`
9292
* from `graphql-js`.
9393
*/
94-
customExecuteFn?: (
95-
args: ExecutionArgs,
96-
) => ExecutionResult | Promise<ExecutionResult>,
94+
customExecuteFn?: (args: ExecutionArgs) => MaybePromise<ExecutionResult>,
9795

9896
/**
9997
* An optional function which will be used to format any errors produced by
@@ -126,7 +124,7 @@ export type OptionsData = {|
126124
*/
127125
extensions?: (
128126
info: RequestInfo,
129-
) => { [key: string]: mixed, ... } | Promise<{ [key: string]: mixed, ... }>,
127+
) => MaybePromise<void | { [key: string]: mixed, ... }>,
130128

131129
/**
132130
* A boolean to optionally enable GraphiQL mode.
@@ -347,16 +345,16 @@ export function graphqlHTTP(options: Options): Middleware {
347345
// Collect and apply any metadata extensions if a function was provided.
348346
// https://graphql.github.io/graphql-spec/#sec-Response-Format
349347
if (extensionsFn) {
350-
const extensionsObj = await extensionsFn({
348+
const extensions = await extensionsFn({
351349
document: documentAST,
352350
variables,
353351
operationName,
354352
result,
355353
context,
356354
});
357355

358-
if (extensionsObj != null && typeof extensionsObj === 'object') {
359-
(result: any).extensions = extensionsObj;
356+
if (extensions != null) {
357+
result = { ...result, extensions };
360358
}
361359
}
362360
} catch (error) {

0 commit comments

Comments
 (0)