Created
August 25, 2017 15:07
-
-
Save gc-codesnippets/0fbb5b3c6eee4c3ead146408f60665c3 to your computer and use it in GitHub Desktop.
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
import {commitMutation, graphql} from 'react-relay/compat' | |
const mutation = graphql` | |
mutation ChangeTodoStatusMutation( | |
$input: UpdateTodoInput! | |
) { | |
updateTodo(input: $input) { | |
todo { | |
id | |
complete | |
} | |
edge { | |
node { | |
id | |
complete | |
} | |
} | |
viewer { | |
allTodoes(last: 1000) { | |
edges { | |
node { | |
id | |
complete | |
} | |
} | |
} | |
} | |
} | |
} | |
` | |
function getConfigs(todoId, viewerId) { | |
return [{ | |
type: 'FIELDS_CHANGE', | |
fieldIDs: { | |
todo: todoId, | |
viewer: viewerId, | |
}, | |
}] | |
} | |
function getOptimisticResponse (complete, todoId, viewerId) { | |
const viewerPayload = {id: viewerId} | |
return { | |
todo: { | |
complete, | |
id: todoId, | |
}, | |
viewer: viewerPayload, | |
} | |
} | |
function commit(environment, todo, complete, viewerId) { | |
return commitMutation( | |
environment, | |
{ | |
mutation, | |
variables: {input: { id: todo.id, text: todo.text, complete, clientMutationId: 'asd' }}, | |
configs: getConfigs(viewerId), | |
optimisticResponse: () => getOptimisticResponse(complete, todo.id, viewerId), | |
} | |
) | |
} | |
export default {commit} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment