Skip to content

Instantly share code, notes, and snippets.

@gc-codesnippets
Created August 25, 2017 15:07
Show Gist options
  • Save gc-codesnippets/0fbb5b3c6eee4c3ead146408f60665c3 to your computer and use it in GitHub Desktop.
Save gc-codesnippets/0fbb5b3c6eee4c3ead146408f60665c3 to your computer and use it in GitHub Desktop.
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