Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GQL Subscriptions over WebSockets #3333

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open

GQL Subscriptions over WebSockets #3333

wants to merge 5 commits into from

Conversation

CarsonF
Copy link
Member

@CarsonF CarsonF commented Nov 13, 2024

This PR is pushed the need for #3330

Yoga already has built-in support for Subscriptions over SSE (which I forgot until today).
These work fine when deployed to AWS which uses HTTP/2.
Locally, over HTTP/1, however there is the a 6 connection limit.

Apollo Sandbox/Explorer/Studio also does not support SSE, they have a similar "apollo http multipart" protocol.

Websockets are supported, so this is for them, and just to have flexibility in subscription protocol.
UI client could use either going forward.

How it works

This ties fastify, yoga, and graphql-ws together.
Execution layers in order:

  1. fastify route (http path matching)
  2. fastify's websocket plugin (http upgrade request & websocket open/close)
    This allows our fastify hooks to be executed.
    And provides a consistent Fastify Request type,
    instead of a raw IncomingMessage.
  3. graphql-ws's fastify handler (adapts # 2 to graphql-ws)
  4. graphql-ws (handles specific gql protocol over websockets)
  5. graphql-yoga is unwrapped to envelop.
    Yoga just wraps envelop and handles more of the http layer.
    We really just reference envelop hooks with our "Yoga Plugins".
    So this allows our "yoga" plugins to be executed.

@graphql-yoga/nestjs goes a completely different direction with its subscription logic, which is why I abandoned it. It adapts from the ApolloDriver, doing a bunch of unnecessary low level work. It couldn't assume fastify, because express is also supported.
This approach here fully embraces fastify & yoga, and both of their lifecycle hooks.

@coderabbitai ignore

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for WebSocket connections and GraphQL subscriptions, enhancing real-time capabilities.
    • Introduced new methods to handle subscription operations and errors within the GraphQL context.
  • Bug Fixes

    • Improved error handling for subscription-related issues.
  • Chores

    • Updated project dependencies to include @fastify/websocket and graphql-ws.

subscribe() is called from a socket.onmessage event, which doesn't have the
same async context as the original http (upgrade) request.

Save and resume the context between these two points.

This allows HttpHooks to declare an ALS (like edgedb current user),
and then have these scoped inside GQL subscriptions from websockets.
coderabbitai[bot]

This comment was marked as resolved.

@SeedCompany SeedCompany deleted a comment from coderabbitai bot Nov 13, 2024
@SeedCompany SeedCompany deleted a comment from coderabbitai bot Nov 13, 2024
@SeedCompany SeedCompany deleted a comment from coderabbitai bot Nov 13, 2024
@SeedCompany SeedCompany deleted a comment from coderabbitai bot Nov 13, 2024
@SeedCompany SeedCompany deleted a comment from coderabbitai bot Nov 13, 2024
@SeedCompany SeedCompany deleted a comment from coderabbitai bot Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants