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

docs: update subscribeToMore for getDerivedStateFromProps #3680

Merged
merged 5 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
[@pschreibs85](https://github.com/pschreibs85) in [#3812](https://github.com/apollographql/apollo-client/pull/3812) <br/>
[@msreekm](https://github.com/msreekm) in [#3808](https://github.com/apollographql/apollo-client/pull/3808) <br/>
[@kamaltmo](https://github.com/kamaltmo) in [#3806](https://github.com/apollographql/apollo-client/pull/3806) <br/>
[@lorensr](https://github.com/lorensr) in [#3739](https://github.com/apollographql/apollo-client/pull/3739)
[@lorensr](https://github.com/lorensr) in [#3739](https://github.com/apollographql/apollo-client/pull/3739) <br/>
[@brainkim](https://github.com/brainkim) in [#3680](https://github.com/apollographql/apollo-client/pull/3680)

### Apollo Cache In-Memory (vNext)

Expand Down
49 changes: 31 additions & 18 deletions docs/source/api/react-apollo.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ This function will set up a subscription, triggering updates whenever the server

This function returns an `unsubscribe` function handler which can be used to unsubscribe later.

A common practice is to wrap the `subscribeToMore` call within `componentWillReceiveProps` and perform the subscription after the original query has completed. To ensure the subscription isn't created multiple times, you can attach it to the component instance. See the example for more details.
A common practice is to wrap the `subscribeToMore` call within `getDerivedStateFromProps` and perform the subscription after the original query has completed. To ensure the subscription isn't created multiple times, you can add it to component state. See the example for more details.

- `[document]`: Document is a required property that accepts a GraphQL subscription created with `graphql-tag`’s `gql` template string tag. It should contain a single GraphQL subscription operation with the data that will be returned.
- `[variables]`: The optional variables you may provide that will be used with the `document` option.
Expand All @@ -715,35 +715,48 @@ In order to update the query's store with the result of the subscription, you mu

```js
class SubscriptionComponent extends Component {
componentWillReceiveProps(nextProps) {
if(!nextProps.data.loading) {
state = {
subscriptionParam: null,
unsubscribe: null,
};

static getDerivedStateFromProps(nextProps, prevState) {
if (!nextProps.data.loading) {
// Check for existing subscription
if (this.unsubscribe) {
// Check if props have changed and, if necessary, stop the subscription
if (this.props.subscriptionParam !== nextProps.subscriptionParam) {
this.unsubscribe();
} else {
return;
if (prevState.unsubscribe) {
// Only unsubscribe/update state if subscription variable has changed
if (prevState.subscriptionParam === nextProps.subscriptionParam) {
return null;
}
prevState.unsubscribe();
}

// Subscribe
this.unsubscribe = nextProps.data.subscribeToMore({
document: gql`subscription {...}`,
updateQuery: (previousResult, { subscriptionData, variables }) => {
// Perform updates on previousResult with subscriptionData
return updatedResult;
}
});
return {
// Subscribe
unsubscribe: nextProps.data.subscribeToMore({
document: gql`subscription {...}`,
variables: {
param: nextProps.subscriptionParam,
},
updateQuery: (previousResult, { subscriptionData, variables }) => {
// Perform updates on previousResult with subscriptionData
return updatedResult;
},
}),
// Store subscriptionParam in state for next update
subscriptionParam: nextProps.subscriptionParam,
};
}

return null;
}

render() {
...
}
}
```


<h3 id="graphql-query-data-startPolling">`data.startPolling(interval)`</h3>

This function will set up an interval and send a fetch request every time that interval ellapses. The function takes only one integer argument which allows you to configure how often you want your query to be executed in milliseconds. In other words, the `interval` argument represents the milliseconds between polls.
Expand Down