You can try this, but I don't think you will be able to retain the completed status of true on your server because you are sending a completeTodo mutation with a temporary id. There is now way for your server to know which todo you are referencing. Although this may give you two optimistic updates as you want.
const CREATE_TODO_MUTATION = gql`
mutation createTodo($todoContent: String!) {
createTodo(todoContent: $todoContent) {
id
createdAt
content
completed
}
}
`;
const COMPLETE_TODO_MUTATION = gql`
mutation completeTodo($id: String!) {
completeTodo(id: $id) {
id
createdAt
content
completed
}
}
`;
const TodosPageWithMutations = compose(
graphql(CREATE_TODO_MUTATION, {
props: ({ ownProps, mutate }) => ({
createTodo: content => mutate({
variables: { todoContent: content },
optimisticResponse: {
__typename: 'Mutation',
createTodo: {
__typename: 'Todo',
id: createTempID(),
content,
completed: false,
createdAt: new Date()
}
}
}),
}),
}),
graphql(COMPLETE_TODO_MUTATION, {
props: ({ ownProps, mutate }) => ({
completeTodo: todo => mutate({
variables: { id: todo.id },
optimisticResponse: {
__typename: 'Mutation',
completeTodo: {
__typename: 'Todo',
id: todo.id,
content: todo.content,
completed: true,
createdAt: todo.createdAt
}
}
}),
}),
})
)(TodosPage);