diff options
| author | Wes Bos <wesbos@gmail.com> | 2017-10-27 10:10:49 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2017-10-27 10:10:49 -0400 |
| commit | acdfa2840f867e686471a8e9ac3f29353fab7dd1 (patch) | |
| tree | 1a069ce11e145d9b286084221951272511debbdf /lib | |
| parent | 6f44adb59d3542eb21e55e0aae789b1fcc7cf8f1 (diff) | |
hi
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/image.js | 7 | ||||
| -rw-r--r-- | lib/initApollo.js | 65 | ||||
| -rw-r--r-- | lib/withData.js | 9 |
3 files changed, 39 insertions, 42 deletions
diff --git a/lib/image.js b/lib/image.js index 40045b3..5b6efb9 100644 --- a/lib/image.js +++ b/lib/image.js @@ -1,5 +1,6 @@ -// TODO: Put the project ID in a config file. +import { imageEndpoint } from '../config'; + export default function(image, width = 300, height = 300) { - if(!image || !image.secret) return ''; - return `https://images.graph.cool/v1/cj5xz8szs28930145gct82bdj/${image.secret}/${width}x${height}`; + if (!image || !image.secret) return ''; + return `${imageEndpoint}/${image.secret}/${width}x${height}`; } diff --git a/lib/initApollo.js b/lib/initApollo.js index de5c7f2..41b058c 100644 --- a/lib/initApollo.js +++ b/lib/initApollo.js @@ -1,57 +1,60 @@ -import { ApolloClient, createNetworkInterface } from 'react-apollo' -import fetch from 'isomorphic-fetch' +// import { ApolloClient, createNetworkInterface } from 'react-apollo'; +import { ApolloClient } from 'apollo-client'; +import fetch from 'isomorphic-fetch'; +import { InMemoryCache } from 'apollo-cache-inmemory'; +import { createHttpLink } from 'apollo-link-http'; +import { ApolloLink } from 'apollo-link'; +import { endpoint } from '../config'; -let apolloClient = null +let apolloClient = null; // Polyfill fetch() on the server (used by apollo-client) if (!process.browser) { - global.fetch = fetch + global.fetch = fetch; } -function create (initialState) { - - const networkInterface = createNetworkInterface({ - uri: 'https://api.graph.cool/simple/v1/cj5xz8szs28930145gct82bdj', // Server URL (must be absolute) - opts: { // Additional fetch() options like `credentials` or `headers` +function create(initialState = {}) { + const httpLink = createHttpLink({ + uri: endpoint, // Server URL (must be absolute) + opts: { + // Additional fetch() options like `credentials` or `headers` credentials: 'same-origin', - } + }, }); - // use the auth0IdToken in localStorage for authorized requests - networkInterface.use([ - { - applyMiddleware(req, next) { - if (!req.options.headers) { - req.options.headers = {}; - } + const middlewareLink = new ApolloLink((operation, forward) => { + const headers = {}; - // get the authentication token from local storage if it exists - if (typeof localStorage !== 'undefined' && localStorage.getItem("auth0IdToken")) { - req.options.headers.authorization = `Bearer ${localStorage.getItem("auth0IdToken")}`; - } - next(); - } + if (typeof localStorage !== 'undefined' && localStorage.getItem('auth0IdToken')) { + headers.authorization = `Bearer ${localStorage.getItem('auth0IdToken')}`; } - ]); + + operation.setContext({ headers }); + return forward(operation); + }); + + const link = middlewareLink.concat(httpLink); return new ApolloClient({ - initialState, + 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) - networkInterface - }) + }); } -export default function initApollo (initialState) { +export default function initApollo(initialState) { // Make sure to create a new client for every server-side request so that data // isn't shared between connections (which would be bad) if (!process.browser) { - return create(initialState) + return create(initialState); } // Reuse client on the client-side if (!apolloClient) { - apolloClient = create(initialState) + apolloClient = create(initialState); } - return apolloClient + return apolloClient; } diff --git a/lib/withData.js b/lib/withData.js index 6b37792..ab9fe78 100644 --- a/lib/withData.js +++ b/lib/withData.js @@ -49,14 +49,7 @@ export default ComposedComponent => Head.rewind(); // Extract query data from the Apollo store - const state = apollo.getInitialState(); - - serverState = { - apollo: { - // Only include the Apollo data state - data: state.data, - }, - }; + serverState = apollo.cache.extract(); } return { |
