Skip to content

Instantly share code, notes, and snippets.

@slmyers
Last active August 12, 2018 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slmyers/8c6f57d2d621a8b0f36b1b1c114ea42b to your computer and use it in GitHub Desktop.
Save slmyers/8c6f57d2d621a8b0f36b1b1c114ea42b to your computer and use it in GitHub Desktop.
Apollo Client Link example
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