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

Allow extensions callback to return undefined #653

Merged
merged 1 commit into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/__tests__/http-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2251,13 +2251,10 @@ function runTests(server: Server) {

app.get(
urlString(),
// @ts-expect-error
graphqlHTTP(() => ({
schema: TestSchema,
context: { foo: 'bar' },
extensions({ context }) {
return () => ({ contextValue: JSON.stringify(context) });
},
extensions: () => undefined,
})),
);

Expand Down
12 changes: 5 additions & 7 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {};
type Request = IncomingMessage;

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

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

Expand All @@ -38,9 +39,8 @@ export type Options =
request: Request,
response: Response,
params?: GraphQLParams,
) => OptionsResult)
| OptionsResult;
type OptionsResult = OptionsData | Promise<OptionsData>;
) => MaybePromise<OptionsData>)
| MaybePromise<OptionsData>;

export interface OptionsData {
/**
Expand Down Expand Up @@ -83,9 +83,7 @@ export interface OptionsData {
* An optional function which will be used to execute instead of default `execute`
* from `graphql-js`.
*/
customExecuteFn?: (
args: ExecutionArgs,
) => ExecutionResult | Promise<ExecutionResult>;
customExecuteFn?: (args: ExecutionArgs) => MaybePromise<ExecutionResult>;

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

/**
* A boolean to optionally enable GraphiQL mode.
Expand Down
18 changes: 8 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { renderGraphiQL } from './renderGraphiQL';

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

/**
* Used to configure the graphqlHTTP middleware by providing a schema
Expand All @@ -46,9 +47,8 @@ export type Options =
request: $Request,
response: $Response,
params?: GraphQLParams,
) => OptionsResult)
| OptionsResult;
export type OptionsResult = OptionsData | Promise<OptionsData>;
) => MaybePromise<OptionsData>)
| MaybePromise<OptionsData>;

export type OptionsData = {|
/**
Expand Down Expand Up @@ -91,9 +91,7 @@ export type OptionsData = {|
* An optional function which will be used to execute instead of default `execute`
* from `graphql-js`.
*/
customExecuteFn?: (
args: ExecutionArgs,
) => ExecutionResult | Promise<ExecutionResult>,
customExecuteFn?: (args: ExecutionArgs) => MaybePromise<ExecutionResult>,

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

/**
* A boolean to optionally enable GraphiQL mode.
Expand Down Expand Up @@ -347,16 +345,16 @@ export function graphqlHTTP(options: Options): Middleware {
// Collect and apply any metadata extensions if a function was provided.
// https://graphql.github.io/graphql-spec/#sec-Response-Format
if (extensionsFn) {
const extensionsObj = await extensionsFn({
const extensions = await extensionsFn({
document: documentAST,
variables,
operationName,
result,
context,
});

if (extensionsObj != null && typeof extensionsObj === 'object') {
(result: any).extensions = extensionsObj;
if (extensions != null) {
result = { ...result, extensions };
}
}
} catch (error) {
Expand Down