Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

docs(core): Update subscription instructions (#3127)

authored by

Phil Pluckthun and committed by
GitHub
29ad3d63 b7b14a40

+20 -7
+20 -7
docs/advanced/subscriptions.md
··· 34 34 In the above example, we add the `subscriptionExchange` to the `Client` with the default exchanges 35 35 added before it. The `subscriptionExchange` is a factory that accepts additional options and returns 36 36 the actual `Exchange` function. It does not make any assumption over the transport protocol and 37 - scheme that is used. Instead, we need to pass a `forwardSubscription` function, which is called with 38 - an "enriched" _Operation_ every time the `Client` attempts to execute a GraphQL Subscription. 37 + scheme that is used. Instead, we need to pass a `forwardSubscription` function. 38 + 39 + The `forwardSubscription` is called when the `subscriptionExchange` receives an `Operation`, so 40 + typically, when you’re executing a GraphQL subscription. This will call the `forwardSubscription` 41 + function with a GraphQL request body, in the same shape that a GraphQL HTTP API may receive it as 42 + JSON input. 43 + 44 + If you’re using TypeScript, you may notice that the input that `forwardSubscription` receives has 45 + an optional `query` property. This is because of persisted query support. For some transports, the 46 + `query` property may have to be defaulted to an empty string, which matches the GraphQL over HTTP 47 + specification more closely. 39 48 40 49 When we define this function it must return an "Observable-like" object, which needs to follow the 41 50 [Observable spec](https://github.com/tc39/proposal-observable), which comes down to having an ··· 59 68 cacheExchange, 60 69 fetchExchange, 61 70 subscriptionExchange({ 62 - forwardSubscription: request => ({ 63 - subscribe: sink => ({ 64 - unsubscribe: wsClient.subscribe(request, sink), 65 - }), 66 - }), 71 + forwardSubscription(request) { 72 + const input = { ...request, query: request.query || '' }; 73 + return { 74 + subscribe(sink) { 75 + const unsubscribe = wsClient.subscribe(input, sink); 76 + return { unsubscribe }; 77 + }, 78 + }; 79 + }, 67 80 }), 68 81 ], 69 82 });