Utilizing SSE (Server Side Events) to support GraphQL Schema Auto Refresh #1599
Replies: 2 comments 1 reply
-
@dannysofftie This is an amazing breakdown! This can practically be included to the docs almost as-is 😄 It's worth mentioning @benjie here as well as he pioneered this idea. One part that is still missing is the part about how to reload the schema without actually restarting the GraphQL server. The ideal development experience I envision should be:
Honestly I've not been able to come up with an ideal solution to this problem myself. The solution I came up with involves watching the files for the schema using a watcher like |
Beta Was this translation helpful? Give feedback.
-
I was working for a quick solution to complement my development workflows. In development the server has to restart to compile the schema whenever it changes, but this won't be always the case. Am really curious to hear @benjie 's thoughts on this approach. |
Beta Was this translation helpful? Give feedback.
-
Following Sir Muel's Medium post,
I will break down how to implement SSE on a GraphQL server. I will be using:
Enable GraphQL Event Stream
The first step to make SSE for GraphQL work is to add support for
X-GraphQL-Event-Stream
header. Ideally, your server would respond with this header for every request, to inform your GraphQL IDEs that the supports SSEs.Using Fastify, below
onRequest
hook appendsX-GraphQL-Event-Stream
to responseTwo things happen above:
X-GraphQL-Event-Stream
header sets a valid stream URL for all. Ideally aGET
endpoint that you'd have to implement on your server. (Discussed below)Access-Control-Expose-Headers
header forX-GraphQL-Event-Stream
to allow browser clients to be able to pick it up.With the server informing GraphQL clients for SSE support, you'd need to implement a
GET
endpoint as mentioned in1
above.Implement
GET
endpoint stream URLContinuation using Fastify
Implement SSE support
I must tag PostGraphile, instant lightning-fast GraphQL API backed primarily by your PostgreSQL database. It has a great implementation of SSE, I have borrowed a lot, actually I have borrowed their implementation.
Here is their implementation.
This function will work with Express.js, Koa, Fastify and other Node.js frameworks.
Working with this function, our
GET
implementation would look like belowBeta Was this translation helpful? Give feedback.
All reactions