Last active
August 12, 2018 01:15
-
-
Save slmyers/8c6f57d2d621a8b0f36b1b1c114ea42b to your computer and use it in GitHub Desktop.
Apollo Client Link example
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 gql from 'graphql-tag'; | |
export const defaults = { | |
messages: [] | |
}; | |
export const resolvers = { | |
Mutation: { | |
addMessage: (_, { content, author }, { cache }) => { | |
const query = gql` | |
query GetMessages { | |
messages @client { | |
author, | |
content, | |
id | |
} | |
} | |
`; | |
const { messages } = cache.readQuery({ query }); | |
const last = messages[messages.length - 1] || { id: 0 }; | |
const newMessage = { | |
author, | |
content, | |
__typename: 'Message', | |
id: last.id + 1, | |
}; | |
cache.writeData({ data: { messages: [...messages].concat([newMessage])}}); | |
return newMessage; | |
}, | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment