Skip to content

Commit

Permalink
Do not pass parameters if we can't parse them.
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 1, 2020
1 parent 2819666 commit bc492e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
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

0 comments on commit bc492e4

Please sign in to comment.