summaryrefslogtreecommitdiffstats
path: root/frontend/lib/withData.js
blob: 0696333232d528177a601172f48d9a3b9c3dae59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import withApollo from 'next-with-apollo';
import ApolloClient from 'apollo-boost';
import { LOCAL_STATE_QUERY } from '../queries/queries.graphql';
// can also be a function that accepts a `headers` object (SSR only) and returns a config

const client = new ApolloClient({
  uri: process.env.NODE_ENV === 'development' ? 'http://localhost:4444' : 'http://localhost:4444',
  ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
  // TODO this is a bug: https://github.com/apollographql/apollo-client/issues/3265
  fetchOptions: {
    credentials: 'include',
  },
  request: async operation => {
    operation.setContext({
      fetchOptions: {
        credentials: 'include',
      },
    });
  },
  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 default withApollo({ client });