diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-05-16 12:27:21 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-05-16 12:27:21 -0400 |
| commit | 18752008f9abd7749f61f8d674f66de2a535c131 (patch) | |
| tree | 5825328e58cf8a4a196532f8b40d515b185cd8ea /frontend/lib/withData.js | |
| parent | 802b65cfd634305f62d88cb57c54f8c053701d27 (diff) | |
SSR with Auth
Diffstat (limited to 'frontend/lib/withData.js')
| -rw-r--r-- | frontend/lib/withData.js | 65 |
1 files changed, 31 insertions, 34 deletions
diff --git a/frontend/lib/withData.js b/frontend/lib/withData.js index 9bd22f0..585cee7 100644 --- a/frontend/lib/withData.js +++ b/frontend/lib/withData.js @@ -3,41 +3,38 @@ 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, forward) => { - operation.setContext({ - fetchOptions: { - credentials: 'include', - }, - headers: { - haha: `lol${Date.now()}`, - cookie: '', - // TODO: This cookie needs to come from the SSR - // cookie:'token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjamY0ZWtqcm55ZGQ0MDkxNnhweXEweGVqIiwiaWF0IjoxNTI2NDc4NzY2fQ.LJbLhKZ6j5d3ntDvaBR4V-4rFEPv_pzJiIhZ84WkO6A', - }, - }); - }, - clientState: { - resolvers: { - Mutation: { - toggleCart(_, variables, { cache }) { - const { cartOpen } = cache.read({ query: LOCAL_STATE_QUERY }); - const data = { data: { cartOpen: !cartOpen } }; - cache.writeData(data); - return data; +function createClient({ headers }) { + return 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', }, - }, + headers, + }); }, - defaults: { - cartOpen: false, + 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 }); +export default withApollo({ client: createClient }); |
