diff --git a/package.json b/package.json index 7fb91a65..56620159 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "lib": "./dist" }, "files": [ + "src", "dist", "README.md", "LICENSE", @@ -40,6 +41,7 @@ "lint": "eslint src", "check": "flow check", "build": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist", + "postinstall": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist", "watch": "babel --optional runtime resources/watch.js | node", "cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha", "cover:lcov": "babel-node node_modules/.bin/isparta cover --root src --report lcovonly node_modules/.bin/_mocha -- $npm_package_options_mocha", @@ -66,7 +68,7 @@ "express": "4.13.3", "express3": "*", "flow-bin": "0.18.1", - "graphql": "0.4.8", + "graphql": "0.4.14", "isparta": "3.0.3", "mocha": "2.2.5", "multer": "1.0.3", diff --git a/src/__tests__/http-test.js b/src/__tests__/http-test.js index 31878dbf..97a1e149 100644 --- a/src/__tests__/http-test.js +++ b/src/__tests__/http-test.js @@ -793,6 +793,26 @@ describe('test harness', () => { }); describe('Error handling functionality', () => { + it('call errorsCallback on errors', async done => { + var app = express(); + + app.use(urlString(), graphqlHTTP({ + schema: TestSchema, + pretty: true + }, errors => { + expect(errors).to.have.length(1); + let error = errors[0]; + expect(error.message).to.equal('Throws!'); + expect(error.originalError).to.be.an('object'); + done(); + })); + + await request(app) + .get(urlString({ + query: '{thrower}', + })); + }); + it('handles field errors caught by GraphQL', async () => { var app = express(); diff --git a/src/index.js b/src/index.js index 38258bc3..f7de37f0 100644 --- a/src/index.js +++ b/src/index.js @@ -51,7 +51,7 @@ type Middleware = (request: Request, response: Response) => void; * Middleware for express; takes an options object or function as input to * configure behavior, and returns an express middleware. */ -export default function graphqlHTTP(options: Options): Middleware { +export default function graphqlHTTP(options: Options, errorsCallback: ?Function): Middleware { if (!options) { throw new Error('GraphQL middleware requires options.'); } @@ -169,8 +169,14 @@ export default function graphqlHTTP(options: Options): Middleware { response.status(error.status || 500); return { errors: [ error ] }; }).then(result => { + // Format any encountered errors. if (result && result.errors) { + // Call errorsCallback if errors + if (errorsCallback ) { + errorsCallback(result.errors); + } + result.errors = result.errors.map(formatError); }