summaryrefslogtreecommitdiffstats
path: root/lib/initApollo.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-01-30 12:39:31 -0500
committerWes Bos <wesbos@gmail.com>2018-01-30 12:39:31 -0500
commit0a1648bf47490b312169c531aeb51ef4725e8d2e (patch)
treea9c3dd7b50bc8dd563d9efeee3eeafaca1b3019a /lib/initApollo.js
parent0f1b9240bbd05b377e868faf78b89b509a3d7537 (diff)
Saying goodbye to graphcool
Diffstat (limited to 'lib/initApollo.js')
-rw-r--r--lib/initApollo.js28
1 files changed, 15 insertions, 13 deletions
diff --git a/lib/initApollo.js b/lib/initApollo.js
index 41b058c..35f5e08 100644
--- a/lib/initApollo.js
+++ b/lib/initApollo.js
@@ -1,9 +1,8 @@
-// import { ApolloClient, createNetworkInterface } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
-import fetch from 'isomorphic-fetch';
+import { ApolloLink, concat } from 'apollo-link';
+import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
-import { createHttpLink } from 'apollo-link-http';
-import { ApolloLink } from 'apollo-link';
+import fetch from 'isomorphic-fetch';
import { endpoint } from '../config';
let apolloClient = null;
@@ -13,16 +12,22 @@ if (!process.browser) {
global.fetch = fetch;
}
-function create(initialState = {}) {
- const httpLink = createHttpLink({
+function create(initialState) {
+ // First we create three peices of middleware
+
+ // 1. Create a link for local state to be managed in Apollo
+
+
+ // 1. connect to our GraphQL API
+ const httpLink = new HttpLink({
uri: endpoint, // Server URL (must be absolute)
opts: {
- // Additional fetch() options like `credentials` or `headers`
credentials: 'same-origin',
},
});
- const middlewareLink = new ApolloLink((operation, forward) => {
+ // 2. One to tack on our auth token on each request
+ const authMiddleware = new ApolloLink((operation, forward) => {
const headers = {};
if (typeof localStorage !== 'undefined' && localStorage.getItem('auth0IdToken')) {
@@ -33,14 +38,11 @@ function create(initialState = {}) {
return forward(operation);
});
- const link = middlewareLink.concat(httpLink);
-
return new ApolloClient({
- link,
connectToDevTools: process.browser,
- cache: new InMemoryCache().restore(initialState || {}),
- dataIdFromObject: o => o.id,
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
+ link: concat(authMiddleware, httpLink),
+ cache: new InMemoryCache().restore(initialState || {}),
});
}