GraphQL subscriptions API #2352
Replies: 2 comments 1 reply
-
Question: Using
|
Beta Was this translation helpful? Give feedback.
-
API design question: since you subscribe directly on Anyway, an immediate question arises (for me): what's the difference (in the API) between the service object and the pubsub object. And it seems one could be unnecessary. But most probably I lack some context or something 😄 |
Beta Was this translation helpful? Give feedback.
-
With the introduction of WebSocket support (#156), it's time to draft the GraphQL subscriptions API in MSW. I'd like to use this document to propose a public API and have any related discussions with the community.
Proposed API
Usage
First, you create a GraphQL link via
graphql.link()
. Only GraphQL links will get the.subscription()
method. This aligns with our direction to drop link-less GraphQL support (#1892) and also acts as a requirement for a granular WebSocket endpoint interception.From here, you have multiple ways to interact with subscriptions:
.subscription(queryName, resolver)
. This is handy if you want to react to when your app establishes that subscription. In this scenario, yourresolver
acts as the source of truth that can publish data to that subscription on demand.link.pubsub.publish(event, data)
.Example
Resources
graphql-ws
protocol specificationQuestions
graphql.link()
to stay consistent? Should MSW implicitly add a WebSocket handler to the same link URL but with a switched protocol? Would you prefer a standalonegraphql.wsLink(url)
instead?Beta Was this translation helpful? Give feedback.
All reactions