Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,9 @@ function graphqlHTTP(options: Options): Middleware {
// Then, resolve the Options to get OptionsData.
return resolveOptions(params);
},
(error) => {
// When we failed to parse the GraphQL parameters, we still need to get
// the options object, so make an options call to resolve just that.
const dummyParams = {
query: null,
variables: null,
operationName: null,
raw: null,
};
return resolveOptions(dummyParams).then(() => Promise.reject(error));
},
// When we failed to parse the GraphQL parameters, we still need to get
// the options object, so make an options call to resolve just that.
(error) => resolveOptions().then(() => Promise.reject(error)),
)
.then((optionsData) => {
// Assert that schema is required.
Expand Down Expand Up @@ -402,7 +394,7 @@ function graphqlHTTP(options: Options): Middleware {
}
});

async function resolveOptions(requestParams) {
async function resolveOptions(requestParams?: GraphQLParams) {
const optionsResult =
typeof options === 'function'
? options(request, response, requestParams)
Expand Down Expand Up @@ -438,10 +430,10 @@ function graphqlHTTP(options: Options): Middleware {
}

export type GraphQLParams = {|
query: ?string,
variables: ?{ +[name: string]: mixed, ... },
operationName: ?string,
raw: ?boolean,
query: string | null,
variables: { +[name: string]: mixed, ... } | null,
operationName: string | null,
raw: boolean,
|};

/**
Expand Down Expand Up @@ -496,9 +488,8 @@ function parseGraphQLParams(
* Helper function to determine if GraphiQL can be displayed.
*/
function canDisplayGraphiQL(request: $Request, params: GraphQLParams): boolean {
// If `raw` exists, GraphiQL mode is not enabled.
// Allowed to show GraphiQL if not requested as raw and this request
// prefers HTML over JSON.
// If `raw` false, GraphiQL mode is not enabled.
// Allowed to show GraphiQL if not requested as raw and this request prefers HTML over JSON.
return !params.raw && accepts(request).types(['json', 'html']) === 'html';
}

Expand Down
8 changes: 4 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ declare namespace graphqlHTTP {
) => Promise<undefined>;

interface GraphQLParams {
query: string | null | undefined;
variables: { readonly [name: string]: unknown } | null | undefined;
operationName: string | null | undefined;
raw: boolean | null | undefined;
query: string | null;
variables: { readonly [name: string]: unknown } | null;
operationName: string | null;
raw: boolean | null;
}
}

Expand Down