mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
with-apollo-auth updated for Apollo 2.0 (#3278)
* Updated for Apollo 2.0
* Updated for commit: ccb188a
* Simplified serverState
Updated with danistefanovic's comment. Looks better.
* Revert "Simplified serverState"
This reverts commit 1b543a35909dcfe401c753cb2f71760180087057.
* Simplified server
Updated with Statedanistefanovic's comment.
This commit is contained in:
parent
abe0aebcc0
commit
c6d9ab7563
|
@ -1,4 +1,7 @@
|
||||||
import { ApolloClient, createNetworkInterface } from 'react-apollo'
|
import { ApolloClient } from 'apollo-client'
|
||||||
|
import { createHttpLink } from 'apollo-link-http'
|
||||||
|
import { InMemoryCache } from 'apollo-cache-inmemory'
|
||||||
|
import { setContext } from 'apollo-link-context'
|
||||||
import fetch from 'isomorphic-unfetch'
|
import fetch from 'isomorphic-unfetch'
|
||||||
|
|
||||||
let apolloClient = null
|
let apolloClient = null
|
||||||
|
@ -9,25 +12,26 @@ if (!process.browser) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function create (initialState, { getToken }) {
|
function create (initialState, { getToken }) {
|
||||||
const networkInterface = createNetworkInterface({
|
const httpLink = createHttpLink({
|
||||||
uri: 'https://api.graph.cool/simple/v1/cj3h80ffbllm20162alevpcby'
|
uri: 'https://api.graph.cool/simple/v1/cj5geu3slxl7t0127y8sity9r',
|
||||||
|
credentials: 'same-origin'
|
||||||
})
|
})
|
||||||
|
|
||||||
networkInterface.use([{
|
const authLink = setContext((_, { headers }) => {
|
||||||
applyMiddleware (req, next) {
|
|
||||||
if (!req.options.headers) {
|
|
||||||
req.options.headers = {} // Create the header object if needed.
|
|
||||||
}
|
|
||||||
const token = getToken()
|
const token = getToken()
|
||||||
req.options.headers.authorization = token ? `Bearer ${token}` : null
|
return {
|
||||||
next()
|
headers: {
|
||||||
|
...headers,
|
||||||
|
authorization: token ? `Bearer ${token}` : null
|
||||||
}
|
}
|
||||||
}])
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return new ApolloClient({
|
return new ApolloClient({
|
||||||
initialState,
|
connectToDevTools: process.browser,
|
||||||
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
|
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
|
||||||
networkInterface
|
link: authLink.concat(httpLink),
|
||||||
|
cache: new InMemoryCache().restore(initialState || {})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,14 @@ import React from 'react'
|
||||||
import cookie from 'cookie'
|
import cookie from 'cookie'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { ApolloProvider, getDataFromTree } from 'react-apollo'
|
import { ApolloProvider, getDataFromTree } from 'react-apollo'
|
||||||
|
import Head from 'next/head'
|
||||||
|
|
||||||
import initApollo from './initApollo'
|
import initApollo from './initApollo'
|
||||||
|
|
||||||
function parseCookies (ctx = {}, options = {}) {
|
function parseCookies(context = {}, options = {}) {
|
||||||
return cookie.parse(
|
return cookie.parse(
|
||||||
ctx.req && ctx.req.headers.cookie
|
context.req && context.req.headers.cookie
|
||||||
? ctx.req.headers.cookie
|
? context.req.headers.cookie
|
||||||
: document.cookie,
|
: document.cookie,
|
||||||
options
|
options
|
||||||
)
|
)
|
||||||
|
@ -47,8 +48,8 @@ export default ComposedComponent => {
|
||||||
|
|
||||||
// Provide the `url` prop data in case a graphql query uses it
|
// Provide the `url` prop data in case a graphql query uses it
|
||||||
const url = { query: context.query, pathname: context.pathname }
|
const url = { query: context.query, pathname: context.pathname }
|
||||||
|
try {
|
||||||
// Run all graphql queries
|
// Run all GraphQL queries
|
||||||
const app = (
|
const app = (
|
||||||
<ApolloProvider client={apollo}>
|
<ApolloProvider client={apollo}>
|
||||||
<ComposedComponent url={url} {...composedInitialProps} />
|
<ComposedComponent url={url} {...composedInitialProps} />
|
||||||
|
@ -61,15 +62,17 @@ export default ComposedComponent => {
|
||||||
asPath: context.asPath
|
asPath: context.asPath
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} catch (error) {
|
||||||
|
// Prevent Apollo Client GraphQL errors from crashing SSR.
|
||||||
|
// Handle them in components via the data.error prop:
|
||||||
|
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
|
||||||
|
}
|
||||||
|
// getDataFromTree does not call componentWillUnmount
|
||||||
|
// head side effect therefore need to be cleared manually
|
||||||
|
Head.rewind()
|
||||||
|
|
||||||
// Extract query data from the Apollo's store
|
// Extract query data from the Apollo's store
|
||||||
const state = apollo.getInitialState()
|
serverState = apollo.cache.extract()
|
||||||
|
|
||||||
serverState = {
|
|
||||||
apollo: { // Make sure to only include Apollo's data state
|
|
||||||
data: state.data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -10,13 +10,15 @@
|
||||||
"test": "NODE_ENV=test ava"
|
"test": "NODE_ENV=test ava"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"apollo-client-preset": "1.0.2",
|
||||||
|
"apollo-link-context": "1.0.0",
|
||||||
"cookie": "^0.3.1",
|
"cookie": "^0.3.1",
|
||||||
"graphql": "^0.9.3",
|
"graphql": "0.11.7",
|
||||||
"isomorphic-unfetch": "^2.0.0",
|
"isomorphic-unfetch": "^2.0.0",
|
||||||
"next": "latest",
|
"next": "latest",
|
||||||
"prop-types": "^15.5.10",
|
"prop-types": "^15.5.10",
|
||||||
"react": "^16.0.0",
|
"react": "^16.0.0",
|
||||||
"react-apollo": "^1.1.3",
|
"react-apollo": "2.0.0",
|
||||||
"react-dom": "^16.0.0"
|
"react-dom": "^16.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
Loading…
Reference in a new issue