GraphQL subscriptions API #2352
Replies: 3 comments 2 replies
-
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.
-
Posting as someone with no experience using msw or the graphql test library. I was directed here from this ticket where I discussed improvements to mocking subscriptions for tests. Our team uses Here's one simple example of what one of our subscription tests look like now; it('displays a notification when an alert arrives', async () => {
const testSubscriptionLink = new TestSubscriptionLink();
const mockedResponses: MockedResponse[] = createBackendStateMocks({});
renderPage(
mockedResponses,
testSubscriptionLink,
);
// wait for all the queries to finish loading
expect(await screen.findByLabelText('alert-status')).toBeInTheDocument();
// push an update to the graphql subscription
testSubscriptionLink.pushSubscriptionEvent(
createMockSubscribeAlert({id: '1'}), // this is a `MockedResponse` that the subscription will then push to the `useSubscription` hook data
);
// assert behavior
expect(
await screen.findByText('alert-notification'),
).toBeInTheDocument();
}); In this case, the We have a few complex behaviors that do a lot of cache surgery and state manipulation based on incoming subscription results. We want it to be ergonomic and straightforward to test those in addition to the traditional queries and mutations. We also want to ensure the subscriptions are initialized with the correct arguments and fail the test otherwise. Figuring out how to accomplish those will be the main task in order to fully migrate to this library. All of this commentary may be far too specific and removed from the level of abstraction this library aims to provide, but I hope it helps give some context from one potential user. |
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