-
-
Notifications
You must be signed in to change notification settings - Fork 450
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
V1 subscriptions #160
Merged
Merged
V1 subscriptions #160
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
## Urql Subscriptions | ||
|
||
Urql holds a transport-agnostic opinion and API on subscriptions because the GraphQL spec does not define subscriptions yet. Urql will handle the internal | ||
state and updates, while delegating the actual request operation to a third part (e.g., `subscriptions-transport-ws`). | ||
|
||
There are a few steps to make subscriptions work: | ||
|
||
1. Create a client with a `forwardSubscription` option. | ||
1. pass subscriptions into a Connect component or HOC configuration. | ||
1. pass an `updateSubscription` method to handle the merging of state. | ||
|
||
In more detail: | ||
|
||
### Creating a client | ||
|
||
To get started, as usual, you will want to create a client but make sure to pass in the `forwardSubscription` handler. It has a method signature of: | ||
|
||
```jsx | ||
type forwardSubscription = ( | ||
operation: Operation, | ||
observer: Observer | ||
) => { unsubscribe: () => void }; | ||
``` | ||
|
||
This works out especially well with Apollo's `subscriptions-transport-ws` as it returns the same unsubscribe shape. Here's an example of it: | ||
|
||
```jsx | ||
import { createClient, createQuery } from 'urql'; | ||
import { SubscriptionClient } from 'subscriptions-transport-ws'; | ||
const subscriptionClient = new SubscriptionClient( | ||
'ws://localhost:3001/graphql', | ||
{} | ||
); | ||
|
||
const client = createClient({ | ||
url: 'https://my-host/graphql', | ||
forwardSubscription(operation, observer) => subscribe.request(operation).subscribe({ | ||
next: (data) => observer.next({data, error: null, operation}), | ||
error: (data) => observer.error({data: null, error, operation}), | ||
}) | ||
}); | ||
``` | ||
|
||
### Component/HOC definitions | ||
|
||
Subscriptions are an array for multiple subscriptions a component can listen to. In an example todo app, a subscription for when a todo is added will look like this: | ||
|
||
```jsx | ||
import { createSubscription, Connect, ConnectHOC } from 'urql'; | ||
|
||
const subscriptions = [createSubscription(TodoAdded)]; | ||
|
||
// <Connect {...subscriptions} /> | ||
// or | ||
// ConnectHOC({subscriptions}) | ||
``` | ||
|
||
### updateSubscription handler | ||
|
||
When a subscription receives a new event from the server, it is up to the user to manage the merging of old state, with the new change. This handler is attached to the component/HOC and looks like this: | ||
|
||
```jsx | ||
<Connect | ||
subscriptions={[ | ||
createSubscription(TodoAdded), | ||
createSubscription(TodoRemoved), | ||
]} | ||
updateSubscription={(type, state, data) => { | ||
if (type === 'todoAdded') { | ||
state.push(data); | ||
return state; | ||
} | ||
|
||
if (type === 'todoRemoved') { | ||
state.splice(state.indexOf(data), 1); | ||
return state; | ||
} | ||
}} | ||
/> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
### Description | ||
|
||
Create a [Subscription](../types/mutation-query-subscription.md) from a GraphQL query. | ||
|
||
### Arguments | ||
|
||
| Name | Type | Description | | ||
| --------- | ------- | ---------------------------------------- | | ||
| query | string | A GraphQL subscription string. | | ||
| variables | object? | A collection of GraphQL query variables. | | ||
|
||
### Example | ||
|
||
```jsx | ||
import { createSubscription } from 'urql'; | ||
|
||
const subscriptionString = ` | ||
subscription { | ||
todoAdded { | ||
id | ||
text | ||
} | ||
} | ||
`; | ||
|
||
const subscription = createSubscription(subscriptionString); | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether it makes sense to have options like this on the client. Wouldn't these options be mostly forwarded to exchanges' factory functions? It might be worth to keep as many exchanges as possible opt-in only