cloudflare-worker-graphql-ws-template
cloudflare-worker-graphql-ws-template copied to clipboard
A template for WebSockets powered Cloudflare Worker project using graphql-ws
🚡 graphql-ws on Cloudflare Workers
A template for WebSockets powered Cloudflare Worker project using graphql-ws.
The worker serves the following routes:
"/"renders GraphiQL using onlygraphql-ws(as showcased here)"/graphql"serves the GraphQL over WebSocket
Getting started
This template is meant to be used with Wrangler. If you are not already familiar with the tool, we recommend that you install the tool and configure it to work with your Cloudflare account. Documentation can be found here.
To generate using Wrangler, run this command:
wrangler generate my-graphql-ws https://github.com/enisdenjo/cloudflare-worker-graphql-ws-template
Gotchas
-
Server WebSocket instance does not contain the
protocolproperty (aswsdoes) which is why you should pass theSec-WebSocket-Protocolheader to thegraphql-wsserver -
Message listener event
dataproperty is already a string -
If the client does not support the
"graphql-transport-ws"subprotocol, theSec-WebSocket-Protocolheader will be omitted. This causes Chrome to abruptly terminate the connection with a1006close event code. -
webpack.config.jsis configured to omit thebrowserentry field inpackage.jsons since we are bundling for Node workers and not for browsersThis is especially necessary for
graphql-wssince thebrowserbundle does NOT contain any server code.