From d22616d5a675f5a381c741784333151255713b45 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 6 Apr 2018 16:44:50 -0400 Subject: local data with apollo --- frontend/lib/withData.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'frontend/lib') diff --git a/frontend/lib/withData.js b/frontend/lib/withData.js index c823e62..93a0db1 100644 --- a/frontend/lib/withData.js +++ b/frontend/lib/withData.js @@ -1,6 +1,6 @@ import withApollo from 'next-with-apollo'; import ApolloClient from 'apollo-boost'; - +import { LOCAL_STATE_QUERY } from '../queries/index'; // can also be a function that accepts a `headers` object (SSR only) and returns a config const client = new ApolloClient({ @@ -16,6 +16,21 @@ const client = new ApolloClient({ }); } }, + clientState: { + resolvers: { + Mutation: { + toggleCart(_, variables, { cache }) { + const { cartOpen } = cache.read({ query: LOCAL_STATE_QUERY }); + const data = { data: { cartOpen: !cartOpen } }; + cache.writeData(data); + return data; + }, + }, + }, + defaults: { + cartOpen: false, + }, + }, }); export { client }; -- cgit v1.3