diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-06-14 16:44:21 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-06-14 16:44:21 -0400 |
| commit | b1600adec47a04f60e1da07d94a3ed3906ff5aee (patch) | |
| tree | 828f786da6806d7237ee5cbc5df61e552353b85b /finished-application/frontend/lib/withData.js | |
| parent | a95e08cd2dd5d7ec0a0412adae02515a5f9cec4f (diff) | |
starter files
Diffstat (limited to 'finished-application/frontend/lib/withData.js')
| -rw-r--r-- | finished-application/frontend/lib/withData.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/finished-application/frontend/lib/withData.js b/finished-application/frontend/lib/withData.js new file mode 100644 index 0000000..0256ba9 --- /dev/null +++ b/finished-application/frontend/lib/withData.js @@ -0,0 +1,35 @@ +import withApollo from 'next-with-apollo'; +import ApolloClient from 'apollo-boost'; +import { LOCAL_STATE_QUERY } from '../components/Cart'; +import { endpoint } from '../config'; + +function createClient({ headers }) { + return new ApolloClient({ + uri: process.env.NODE_ENV === 'development' ? endpoint : endpoint, + request: operation => { + operation.setContext({ + fetchOptions: { + credentials: 'include', + }, + headers, + }); + }, + clientState: { + resolvers: { + Mutation: { + toggleCart(_, variables, { cache }) { + const { cartOpen } = cache.readQuery({ query: LOCAL_STATE_QUERY }); + const data = { data: { cartOpen: !cartOpen } }; + cache.writeData(data); + return data; + }, + }, + }, + defaults: { + cartOpen: false, + }, + }, + }); +} + +export default withApollo(createClient); |
