diff --git a/README.md b/README.md index 46e55745..1202d3dc 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ The `graphqlHTTP` function accepts the following options: * **`validationRules`**: Optional additional validation rules queries must satisfy in addition to those defined by the GraphQL spec. + * **`handleErrors`**: An optional function which will be used to handle any errors + encountered by fulfilling a GraphQL operation. If no function is provided, Errors + will default to normal behavior and return in `errors` array. ## HTTP Usage diff --git a/src/index.js b/src/index.js index 826042fa..c6e66855 100644 --- a/src/index.js +++ b/src/index.js @@ -74,6 +74,13 @@ export type OptionsData = { * A boolean to optionally enable GraphiQL mode. */ graphiql?: ?boolean, + + /** + * An optional function which will be used to handle any errors encountered + * by fulfilling a GraphQL operation. If no function is provided, Errors will + * be handled by default. + */ + handleErrors?: ?Function, }; type Middleware = (request: Request, response: Response) => Promise; @@ -101,6 +108,7 @@ export default function graphqlHTTP(options: Options): Middleware { let variables; let operationName; let validationRules; + let handleErrorsFn; // Promises are used as a mechanism for capturing any thrown errors during // the asynchronous process below. @@ -135,6 +143,7 @@ export default function graphqlHTTP(options: Options): Middleware { pretty = optionsData.pretty; graphiql = optionsData.graphiql; formatErrorFn = optionsData.formatError; + handleErrorsFn = optionsData.handleErrors; validationRules = specifiedRules; if (optionsData.validationRules) { @@ -233,6 +242,7 @@ export default function graphqlHTTP(options: Options): Middleware { // Format any encountered errors. if (result && result.errors) { result.errors = result.errors.map(formatErrorFn || formatError); + if (handleErrorsFn) { handleErrorsFn(result, response); } } // If allowed to show GraphiQL, present it instead of JSON. if (showGraphiQL) {