Skip to content

Commit

Permalink
feat(graphiql): Set 'X-GraphQL-Event-Stream' header when --watch enab…
Browse files Browse the repository at this point in the history
…led (#728)

* Dev script assumes less

* Don't support event stream unless watchPg

* Set header pointing to event stream
  • Loading branch information
benjie authored Mar 29, 2018
1 parent ecc0159 commit 5fd2cf2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion scripts/dev
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $npm_bin/nodemon \
--ignore __mocks__ \
--ignore src/postgraphile/graphiql \
--ext js,ts \
--exec "$npm_bin/ts-node --ignore node_modules --disableWarnings src/postgraphile/cli.ts --schema a,b,c --show-error-stack json --watch $@" &
--exec "$npm_bin/ts-node --ignore node_modules --disableWarnings src/postgraphile/cli.ts --show-error-stack json --extended-errors hint,detail,errcode $@" &

# Ensure forked process is killed even if we die unexpectedly
trap 'trap - SIGINT SIGTERM EXIT; JOBS="$(jobs -p)"; [[ "$JOBS" != "" ]] && kill $JOBS 2>/dev/null || true' SIGINT SIGTERM EXIT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export default function createPostGraphileHttpRequestHandler(options) {

// Setup an event stream so we can broadcast events to graphiql, etc.
if (parseUrl(req).pathname === '/_postgraphile/stream') {
if (req.headers.accept !== 'text/event-stream') {
if (!options.watchPg || req.headers.accept !== 'text/event-stream') {
res.statusCode = 405
res.end()
return
Expand Down Expand Up @@ -339,6 +339,11 @@ export default function createPostGraphileHttpRequestHandler(options) {

// If we didn’t call `next` above, all requests will return 200 by default!
res.statusCode = 200
if (options.watchPg) {
// Inform GraphiQL and other clients that they can subscribe to events
// (such as the schema being updated) at the following URL
res.setHeader('X-GraphQL-Event-Stream', '/_postgraphile/stream')
}

// Don’t execute our GraphQL stuffs for `OPTIONS` requests.
if (req.method === 'OPTIONS') {
Expand Down

0 comments on commit 5fd2cf2

Please sign in to comment.