From bc492e493ba1bd7aa0ba6ab384a05a60596d3cc1 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 1 Jun 2020 02:50:17 +0300 Subject: [PATCH] Do not pass parameters if we can't parse them. --- src/index.js | 29 ++++++++++------------------- types/index.d.ts | 8 ++++---- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/index.js b/src/index.js index c9b72178..a319b447 100644 --- a/src/index.js +++ b/src/index.js @@ -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. @@ -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) @@ -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, |}; /** @@ -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'; } diff --git a/types/index.d.ts b/types/index.d.ts index b9d6f529..634622c8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -185,10 +185,10 @@ declare namespace graphqlHTTP { ) => Promise; 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; } }