diff --git a/README.md b/README.md index aa25055e..641622df 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. + * **`extensions`**: An optional function for adding additional metadata to the GraphQL response as a key-value object. The result will be added to `"extensions"` field in the resulting JSON. This is often a useful place to diff --git a/src/index.js b/src/index.js index bb739bdb..c0ded311 100644 --- a/src/index.js +++ b/src/index.js @@ -75,6 +75,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 array of validation rules that will be applied on the document * in additional to those defined by the GraphQL spec. @@ -196,6 +203,7 @@ function graphqlHTTP(options: Options): Middleware { let context; let params; let pretty; + let httpErrorOnEmptyData; let formatErrorFn = formatError; let validateFn = validate; let executeFn = execute; @@ -244,6 +252,7 @@ function graphqlHTTP(options: Options): Middleware { const typeResolver = optionsData.typeResolver; const graphiql = optionsData.graphiql; context = optionsData.context || request; + httpErrorOnEmptyData = optionsData.httpErrorOnEmptyData !== false; let validationRules = specifiedRules; if (optionsData.validationRules) { @@ -375,7 +384,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 (response.statusCode === 200 && result && !result.data) { + if ( + httpErrorOnEmptyData && + response.statusCode === 200 && + result && + !result.data + ) { response.statusCode = 500; } // Format any encountered errors.