Skip to content

Instantly share code, notes, and snippets.

@xavxyz
Created November 20, 2017 11:00
Show Gist options
  • Save xavxyz/4dfbb96c54491f6c1b82d45dd40c2817 to your computer and use it in GitHub Desktop.
Save xavxyz/4dfbb96c54491f6c1b82d45dd40c2817 to your computer and use it in GitHub Desktop.
Snippet from the Advanced GraphQL workshop I run at OK GROW! - https://www.okgrow.com/graphql
this.props.subscribeToMore({
// a graphql document (`subscriptions
document: LIST_SUBSCRIPTION_UPDATE,
// how to update the cache when new data comes from the subscriptions
updateQuery: (prev, { subscriptionData }) => {
const { placeUpdated } = subscriptionData;
if (!placeUpdated) {
return prev;
}
const index = prev.places.findIndex(
place => place.id === placeUpdated.id
);
// change the "updated place" in the list of places present in the cache
return {
...prev,
places: [
...prev.places.slice(0, index),
placeUpdated,
...prev.places.slice(index + 1),
],
};
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment