summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/49/frontend/lib
diff options
context:
space:
mode:
authorRandy Ridge <randyridge@gmail.com>2018-09-14 20:01:25 -0400
committerGitHub <noreply@github.com>2018-09-14 20:01:25 -0400
commit908180c77cd38011eeedcae949613da2a3391e5e (patch)
treeb59bf1aae819a04d453cde72f6e601f5561a740f /stepped-solutions/49/frontend/lib
parent1f6667d233a4a2e9df977204d83a8f749c050c75 (diff)
parentb3bebda57ba187b7fa10398054b54c05d3fd3555 (diff)
Merge branch 'master' into randyridge/our
Diffstat (limited to 'stepped-solutions/49/frontend/lib')
-rwxr-xr-xstepped-solutions/49/frontend/lib/withData.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/stepped-solutions/49/frontend/lib/withData.js b/stepped-solutions/49/frontend/lib/withData.js
new file mode 100755
index 0000000..27571e7
--- /dev/null
+++ b/stepped-solutions/49/frontend/lib/withData.js
@@ -0,0 +1,42 @@
+import withApollo from 'next-with-apollo';
+import ApolloClient from 'apollo-boost';
+import { endpoint } from '../config';
+import { LOCAL_STATE_QUERY } from '../components/Cart';
+
+function createClient({ headers }) {
+ return new ApolloClient({
+ uri: process.env.NODE_ENV === 'development' ? endpoint : endpoint,
+ request: operation => {
+ operation.setContext({
+ fetchOptions: {
+ credentials: 'include',
+ },
+ headers,
+ });
+ },
+ // local data
+ clientState: {
+ resolvers: {
+ Mutation: {
+ toggleCart(_, variables, { cache }) {
+ // read the cartOpen value from the cache
+ const { cartOpen } = cache.readQuery({
+ query: LOCAL_STATE_QUERY,
+ });
+ // Write the cart State to the opposite
+ const data = {
+ data: { cartOpen: !cartOpen },
+ };
+ cache.writeData(data);
+ return data;
+ },
+ },
+ },
+ defaults: {
+ cartOpen: false,
+ },
+ },
+ });
+}
+
+export default withApollo(createClient);