From ade11bea519bfcf57c21f0ae6e04957e07629797 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Thu, 31 May 2018 16:21:35 +0100 Subject: [PATCH] feat(hooks): add a hook to the request handler (#770) --- package.json | 2 +- .../http/createPostGraphileHttpRequestHandler.js | 11 ++++++++++- yarn.lock | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 046cfdaf73..fc7d7cb37d 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "parseurl": "^1.3.1", "pg": ">=6.1.0 <8", "pg-connection-string": "^0.1.3", - "pg-sql2": "2.0.0", + "pg-sql2": "2.1.0", "postgraphile-core": "4.0.0-beta.7.2", "send": "^0.16.1", "tslib": "^1.5.0" diff --git a/src/postgraphile/http/createPostGraphileHttpRequestHandler.js b/src/postgraphile/http/createPostGraphileHttpRequestHandler.js index 569cf8a1a7..f0debba75c 100644 --- a/src/postgraphile/http/createPostGraphileHttpRequestHandler.js +++ b/src/postgraphile/http/createPostGraphileHttpRequestHandler.js @@ -308,7 +308,16 @@ export default function createPostGraphileHttpRequestHandler(options) { * @param {IncomingMessage} req * @param {ServerResponse} res */ - const requestHandler = async (req, res, next) => { + const requestHandler = async (incomingReq, res, next) => { + // You can use this hook either to modify the incoming request or to tell + // PostGraphile not to handle the request further (return null). NOTE: if + // you return `null` from this hook then you are also responsible for + // calling `next()` (should that be required). + const req = pluginHook('postgraphile:http:handler', incomingReq, {options, res, next}) + if (req == null) { + return + } + // Add our CORS headers to be good web citizens (there are perf // implications though so be careful!) // diff --git a/yarn.lock b/yarn.lock index f4daddda1f..d2762489f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5921,6 +5921,12 @@ pg-sql2@2.0.0: dependencies: debug ">=2 <3" +pg-sql2@2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/pg-sql2/-/pg-sql2-2.1.0.tgz#f86978278451482099e4f8e8db8b7486ecb67e3f" + dependencies: + debug ">=2 <3" + pg-types@~1.12.1: version "1.12.1" resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-1.12.1.tgz#d64087e3903b58ffaad279e7595c52208a14c3d2"