From 00fff47cbe71c97fc2d67db650f04bcb1387f94b Mon Sep 17 00:00:00 2001 From: jkwiecinski Date: Mon, 17 Sep 2018 14:47:16 +0200 Subject: [PATCH 1/5] WIP: allow the user to disable setting the HTTP response code to 500 if the response contains only errors, no data. --- README.md | 2 ++ src/index.js | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b4fa4d0..b56c8f16 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ The `graphqlHTTP` function accepts the following options: * **`pretty`**: If `true`, any JSON response will be pretty-printed. + * **`httpErrorOnEmptyData`**: If `false`, the HTTP status code of the response will not be set to 500 if the response contains only errors, not data. + * **`formatError`**: An optional function which will be used to format any errors produced by fulfilling a GraphQL operation. If no function is provided, GraphQL's default spec-compliant [`formatError`][] function will be used. diff --git a/src/index.js b/src/index.js index bac06fbe..14e1e6fa 100644 --- a/src/index.js +++ b/src/index.js @@ -72,6 +72,13 @@ export type OptionsData = { */ pretty?: ?boolean, + /** + * A boolean to configure whether HTTP status code should be set to 500 + * if the query did not return any data. + * True by default. + */ + httpErrorOnEmptyData?: ?boolean, + /** * An optional function which will be used to format any errors produced by * fulfilling a GraphQL operation. If no function is provided, GraphQL's @@ -79,6 +86,8 @@ export type OptionsData = { */ formatError?: ?(error: GraphQLError) => mixed, + + /** * An optional array of validation rules that will be applied on the document * in additional to those defined by the GraphQL spec. @@ -158,6 +167,7 @@ function graphqlHTTP(options: Options): Middleware { let context; let params; let pretty; + let httpErrorOnEmptyData; let formatErrorFn; let extensionsFn; let showGraphiQL; @@ -203,6 +213,7 @@ function graphqlHTTP(options: Options): Middleware { const graphiql = optionsData.graphiql; context = optionsData.context || request; + httpErrorOnEmptyData = optionsData.httpErrorOnEmptyData !== false; let validationRules = specifiedRules; if (optionsData.validationRules) { @@ -328,7 +339,7 @@ function graphqlHTTP(options: Options): Middleware { // Note: Information about the error itself will still be contained in // the resulting JSON payload. // http://facebook.github.io/graphql/#sec-Data - if (response.statusCode === 200 && result && !result.data) { + if (httpErrorOnEmptyData == && response.statusCode === 200 && result && !result.data) { response.statusCode = 500; } // Format any encountered errors. From 235e2f865afb26a4705100638ca81a7f4b32e73f Mon Sep 17 00:00:00 2001 From: jkwiecinski Date: Mon, 17 Sep 2018 14:49:56 +0200 Subject: [PATCH 2/5] remove unnecessary blank lines --- src/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.js b/src/index.js index 14e1e6fa..44867145 100644 --- a/src/index.js +++ b/src/index.js @@ -86,8 +86,6 @@ export type OptionsData = { */ formatError?: ?(error: GraphQLError) => mixed, - - /** * An optional array of validation rules that will be applied on the document * in additional to those defined by the GraphQL spec. From df973f992e574442dace2b82df7329efce166f4a Mon Sep 17 00:00:00 2001 From: jkwiecinski Date: Mon, 17 Sep 2018 14:53:46 +0200 Subject: [PATCH 3/5] fix: change == to === --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 44867145..d2e58ffb 100644 --- a/src/index.js +++ b/src/index.js @@ -337,7 +337,7 @@ function graphqlHTTP(options: Options): Middleware { // Note: Information about the error itself will still be contained in // the resulting JSON payload. // http://facebook.github.io/graphql/#sec-Data - if (httpErrorOnEmptyData == && response.statusCode === 200 && result && !result.data) { + if (httpErrorOnEmptyData === && response.statusCode === 200 && result && !result.data) { response.statusCode = 500; } // Format any encountered errors. From 132a08c2110c73107e31effbee92aa3886ca6c3c Mon Sep 17 00:00:00 2001 From: jkwiecinski Date: Mon, 17 Sep 2018 15:13:23 +0200 Subject: [PATCH 4/5] typo --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index d2e58ffb..1cffb83a 100644 --- a/src/index.js +++ b/src/index.js @@ -337,7 +337,7 @@ function graphqlHTTP(options: Options): Middleware { // Note: Information about the error itself will still be contained in // the resulting JSON payload. // http://facebook.github.io/graphql/#sec-Data - if (httpErrorOnEmptyData === && response.statusCode === 200 && result && !result.data) { + if (httpErrorOnEmptyData && response.statusCode === 200 && result && !result.data) { response.statusCode = 500; } // Format any encountered errors. From f88872c23becaddbd742efe4b8be4a6108a089a6 Mon Sep 17 00:00:00 2001 From: jkwiecinski Date: Tue, 18 Sep 2018 10:05:59 +0200 Subject: [PATCH 5/5] linter fixes --- src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 1cffb83a..8ba7dd14 100644 --- a/src/index.js +++ b/src/index.js @@ -337,7 +337,12 @@ function graphqlHTTP(options: Options): Middleware { // Note: Information about the error itself will still be contained in // the resulting JSON payload. // http://facebook.github.io/graphql/#sec-Data - if (httpErrorOnEmptyData && response.statusCode === 200 && result && !result.data) { + if ( + httpErrorOnEmptyData && + response.statusCode === 200 && + result && + !result.data + ) { response.statusCode = 500; } // Format any encountered errors.