Created
November 20, 2017 11:00
-
-
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 file contains hidden or 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.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