skevy / graphiql-app

Light, Electron-based Wrapper around GraphiQL
MIT License
2.95k stars 336 forks source link

[Proposal] X-GraphQL-Event-Stream header #120

Open benjie opened 6 years ago

benjie commented 6 years ago

This PR is to open a dialog; I'm not expecting it to be merged without some significant changes as I've kind of gone at the source with a hatchet... πŸ˜‡

When a GraphQL server updates the GraphQL schema it would be nice if GraphiQL automatically reflected this. Over at PostGraphile we implement this with a text/event-stream from the server, which fires a change event when the schema changes. Event streams are perfect for this because they're server-sent events, you don't need to worry about receiving data from the client (because you can't) and they are much simpler and more widely supported than websockets.

I'm proposing that we make this a standard - if a GraphQL server supports updating its GraphQL schema it should create the header X-GraphQL-Event-Stream in response to every GraphQL request, passing as the value the relative or absolute URL to the text/event-stream event stream.

The client can then subscribe to the event stream (which can be consumed with EventSource), and will re-inspect the schema whenever a change event is received.

An example of the event-stream implementation in PostGraphile is here:

https://github.com/graphile/postgraphile/blob/master/src/postgraphile/http/setupServerSentEvents.js https://github.com/graphile/postgraphile/blob/5fd2cf218654e03021ef06ffe8f7556a874906b4/src/postgraphile/http/createPostGraphileHttpRequestHandler.js#L286-L295

I've implemented this in GraphiQL.app in this PR. Though doing so doesn't require much code I went a bit further and made sure that the GraphiQL navigation stack is persisted and fixed some other issues also... At the time I wasn't thinking about sending a PR so it kind of snowballed.

If you're interested in any of this I'm happy to send the changes as separate PRs that can be reviewed individually. I understand reviewing this PR as-is is a lot of work!

PS: a lot of the code in here is MIT licensed by @calebmer; I've modified it to make it work in GraphiQL.app.

πŸ™ Thanks!

benjie commented 6 years ago

PPS: apologies that this PR came through before I'd written a description, I accidentally did Cmd-Enter 😳