Skip to content

Instantly share code, notes, and snippets.

@gc-codesnippets
Created August 25, 2017 15:36
Show Gist options
  • Select an option

  • Save gc-codesnippets/624069731ec7900cc998df8441df2b21 to your computer and use it in GitHub Desktop.

Select an option

Save gc-codesnippets/624069731ec7900cc998df8441df2b21 to your computer and use it in GitHub Desktop.
optimisticUpdater: (proxyStore) => {
// 1 - create the `newPost` as a mock that can be added to the store
const id = 'client:newPost:' + tempID++
const newPost = proxyStore.create(id, 'Post')
newPost.setValue(id, 'id')
newPost.setValue(description, 'description')
newPost.setValue(imageUrl, 'imageUrl')
// 2 - add `newPost` to the store
const viewerProxy = proxyStore.get(viewerId)
const connection = ConnectionHandler.getConnection(viewerProxy, 'ListPage_allPosts')
if (connection) {
ConnectionHandler.insertEdgeAfter(connection, newPost)
}
},
updater: (proxyStore) => {
// 1 - retrieve the `newPost` from the server response
const createPostField = proxyStore.getRootField('createPost')
const newPost = createPostField.getLinkedRecord('post')
// 2 - add `newPost` to the store
const viewerProxy = proxyStore.get(viewerId)
const connection = ConnectionHandler.getConnection(viewerProxy, 'ListPage_allPosts')
if (connection) {
ConnectionHandler.insertEdgeAfter(connection, newPost)
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment