Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
f4d6cbfc
Commit
f4d6cbfc
authored
May 10, 2017
by
Roland Warmerdam
Committed by
Tim Neutkens
May 10, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Many improvements to the Apollo examples (#1905)
* Many improvements to the Apollo examples * Use static properties
parent
3b91355c
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
223 additions
and
92 deletions
+223
-92
initApollo.js
examples/with-apollo-and-redux/lib/initApollo.js
+36
-0
initRedux.js
examples/with-apollo-and-redux/lib/initRedux.js
+39
-0
initStore.js
examples/with-apollo-and-redux/lib/initStore.js
+0
-18
middleware.js
examples/with-apollo-and-redux/lib/middleware.js
+0
-9
reducer.js
examples/with-apollo-and-redux/lib/reducer.js
+0
-7
reducers.js
examples/with-apollo-and-redux/lib/reducers.js
+12
-0
withData.js
examples/with-apollo-and-redux/lib/withData.js
+47
-28
package.json
examples/with-apollo-and-redux/package.json
+7
-4
initApollo.js
examples/with-apollo/lib/initApollo.js
+37
-0
withData.js
examples/with-apollo/lib/withData.js
+38
-22
package.json
examples/with-apollo/package.json
+7
-4
No files found.
examples/with-apollo-and-redux/lib/init
Client
.js
→
examples/with-apollo-and-redux/lib/init
Apollo
.js
View file @
f4d6cbfc
import
{
ApolloClient
,
createNetworkInterface
}
from
'react-apollo'
import
{
ApolloClient
,
createNetworkInterface
}
from
'react-apollo'
import
fetch
from
'isomorphic-fetch'
let
apolloClient
=
null
let
apolloClient
=
null
function
_initClient
(
headers
,
initialState
)
{
// Polyfill fetch() on the server (used by apollo-client)
if
(
!
process
.
browser
)
{
global
.
fetch
=
fetch
}
function
create
()
{
return
new
ApolloClient
({
return
new
ApolloClient
({
initialState
,
ssrMode
:
!
process
.
browser
,
// Disables forceFetch on the server (so queries are only run once)
ssrMode
:
!
process
.
browser
,
dataIdFromObject
:
result
=>
result
.
id
||
null
,
networkInterface
:
createNetworkInterface
({
networkInterface
:
createNetworkInterface
({
uri
:
'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn'
,
uri
:
'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn'
,
// Server URL (must be absolute)
opts
:
{
opts
:
{
// Additional fetch() options like `credentials` or `headers`
credentials
:
'same-origin'
credentials
:
'same-origin'
// Pass headers here if your graphql server requires them
}
}
})
})
})
})
}
}
export
const
initClient
=
(
headers
,
initialState
=
{})
=>
{
export
default
function
initApollo
()
{
// 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
)
{
if
(
!
process
.
browser
)
{
return
_initClient
(
headers
,
initialState
)
return
create
(
)
}
}
// Reuse client on the client-side
if
(
!
apolloClient
)
{
if
(
!
apolloClient
)
{
apolloClient
=
_initClient
(
headers
,
initialState
)
apolloClient
=
create
(
)
}
}
return
apolloClient
return
apolloClient
}
}
examples/with-apollo-and-redux/lib/initRedux.js
0 → 100644
View file @
f4d6cbfc
import
{
createStore
,
combineReducers
,
applyMiddleware
,
compose
}
from
'redux'
import
reducers
from
'./reducers'
let
reduxStore
=
null
// Get the Redux DevTools extension and fallback to a no-op function
let
devtools
=
f
=>
f
if
(
process
.
browser
&&
window
.
__REDUX_DEVTOOLS_EXTENSION__
)
{
devtools
=
window
.
__REDUX_DEVTOOLS_EXTENSION__
()
}
function
create
(
apollo
,
initialState
=
{})
{
return
createStore
(
combineReducers
({
// Setup reducers
...
reducers
,
apollo
:
apollo
.
reducer
()
}),
initialState
,
// Hydrate the store with server-side data
compose
(
applyMiddleware
(
apollo
.
middleware
()),
// Add additional middleware here
devtools
)
)
}
export
default
function
initRedux
(
apollo
,
initialState
)
{
// Make sure to create a new store for every server-side request so that data
// isn't shared between connections (which would be bad)
if
(
!
process
.
browser
)
{
return
create
(
apollo
,
initialState
)
}
// Reuse store on the client-side
if
(
!
reduxStore
)
{
reduxStore
=
create
(
apollo
,
initialState
)
}
return
reduxStore
}
examples/with-apollo-and-redux/lib/initStore.js
deleted
100644 → 0
View file @
3b91355c
import
{
createStore
}
from
'redux'
import
getReducer
from
'./reducer'
import
createMiddleware
from
'./middleware'
let
reduxStore
=
null
export
const
initStore
=
(
client
,
initialState
)
=>
{
let
store
if
(
!
process
.
browser
||
!
reduxStore
)
{
const
middleware
=
createMiddleware
(
client
.
middleware
())
store
=
createStore
(
getReducer
(
client
),
initialState
,
middleware
)
if
(
!
process
.
browser
)
{
return
store
}
reduxStore
=
store
}
return
reduxStore
}
examples/with-apollo-and-redux/lib/middleware.js
deleted
100644 → 0
View file @
3b91355c
import
{
applyMiddleware
,
compose
}
from
'redux'
export
default
function
createMiddleware
(
clientMiddleware
)
{
const
middleware
=
applyMiddleware
(
clientMiddleware
)
if
(
process
.
browser
&&
window
.
devToolsExtension
)
{
return
compose
(
middleware
,
window
.
devToolsExtension
())
}
return
middleware
}
examples/with-apollo-and-redux/lib/reducer.js
deleted
100644 → 0
View file @
3b91355c
import
{
combineReducers
}
from
'redux'
export
default
function
getReducer
(
client
)
{
return
combineReducers
({
apollo
:
client
.
reducer
()
})
}
examples/with-apollo-and-redux/lib/reducers.js
0 → 100644
View file @
f4d6cbfc
export
default
{
example
:
(
state
=
{},
{
type
,
payload
})
=>
{
switch
(
type
)
{
case
'EXAMPLE_ACTION'
:
return
{
...
state
}
default
:
return
state
}
}
}
examples/with-apollo-and-redux/lib/withData.js
View file @
f4d6cbfc
import
'isomorphic-fetch'
import
React
from
'react'
import
React
from
'react'
import
PropTypes
from
'prop-types'
import
{
ApolloProvider
,
getDataFromTree
}
from
'react-apollo'
import
{
ApolloProvider
,
getDataFromTree
}
from
'react-apollo'
import
{
initClient
}
from
'./initClient'
import
initApollo
from
'./initApollo'
import
{
initStore
}
from
'./initStore'
import
initRedux
from
'./initRedux'
export
default
ComposedComponent
=>
{
return
class
WithData
extends
React
.
Component
{
static
displayName
=
`WithData(
${
ComposedComponent
.
displayName
}
)`
static
propTypes
=
{
serverState
:
PropTypes
.
object
.
isRequired
}
export
default
(
Component
)
=>
(
class
extends
React
.
Component
{
static
async
getInitialProps
(
ctx
)
{
static
async
getInitialProps
(
ctx
)
{
const
headers
=
ctx
.
req
?
ctx
.
req
.
headers
:
{}
let
serverState
=
{}
const
client
=
initClient
(
headers
)
const
store
=
initStore
(
client
,
client
.
initialState
)
const
props
=
{
// Evaluate the composed component's getInitialProps()
url
:
{
query
:
ctx
.
query
,
pathname
:
ctx
.
pathname
},
let
composedInitialProps
=
{}
...
await
(
Component
.
getInitialProps
?
Component
.
getInitialProps
(
ctx
)
:
{})
if
(
ComposedComponent
.
getInitialProps
)
{
composedInitialProps
=
await
ComposedComponent
.
getInitialProps
(
ctx
)
}
}
// Run all graphql queries in the component tree
// and extract the resulting data
if
(
!
process
.
browser
)
{
if
(
!
process
.
browser
)
{
const
apollo
=
initApollo
()
const
redux
=
initRedux
(
apollo
)
// Provide the `url` prop data in case a graphql query uses it
const
url
=
{
query
:
ctx
.
query
,
pathname
:
ctx
.
pathname
}
// Run all graphql queries
const
app
=
(
const
app
=
(
<
ApolloProvider
client
=
{
client
}
store
=
{
store
}
>
// No need to use the Redux Provider
<
Component
{...
props
}
/
>
// because Apollo sets up the store for us
<
ApolloProvider
client
=
{
apollo
}
store
=
{
redux
}
>
<
ComposedComponent
url
=
{
url
}
{...
composedInitialProps
}
/
>
<
/ApolloProvider
>
<
/ApolloProvider
>
)
)
await
getDataFromTree
(
app
)
await
getDataFromTree
(
app
)
}
const
state
=
store
.
getState
()
// Extract query data from the store
const
state
=
redux
.
getState
()
return
{
// No need to include other initial Redux state because when it
initialState
:
{
// initialises on the client-side it'll create it again anyway
...
state
,
serverState
=
{
apollo
:
{
apollo
:
{
// Make sure to only include Apollo's data state
data
:
client
.
getInitialState
()
.
data
data
:
state
.
apollo
.
data
}
}
},
}
headers
,
}
...
props
return
{
serverState
,
...
composedInitialProps
}
}
}
}
constructor
(
props
)
{
constructor
(
props
)
{
super
(
props
)
super
(
props
)
this
.
client
=
initClient
(
this
.
props
.
headers
,
this
.
props
.
initialState
)
this
.
apollo
=
initApollo
(
)
this
.
store
=
initStore
(
this
.
client
,
this
.
props
.
initial
State
)
this
.
redux
=
initRedux
(
this
.
apollo
,
this
.
props
.
server
State
)
}
}
render
()
{
render
()
{
return
(
return
(
<
ApolloProvider
client
=
{
this
.
client
}
store
=
{
this
.
store
}
>
// No need to use the Redux Provider
<
Component
{...
this
.
props
}
/
>
// because Apollo sets up the store for us
<
ApolloProvider
client
=
{
this
.
apollo
}
store
=
{
this
.
redux
}
>
<
ComposedComponent
{...
this
.
props
}
/
>
<
/ApolloProvider
>
<
/ApolloProvider
>
)
)
}
}
}
}
)
}
examples/with-apollo-and-redux/package.json
View file @
f4d6cbfc
{
{
"name"
:
"with-apollo-and-redux"
,
"name"
:
"with-apollo-and-redux"
,
"version"
:
"
1
.0.0"
,
"version"
:
"
2
.0.0"
,
"scripts"
:
{
"scripts"
:
{
"dev"
:
"next"
,
"dev"
:
"next"
,
"build"
:
"next build"
,
"build"
:
"next build"
,
"start"
:
"next start"
"start"
:
"next start"
},
},
"dependencies"
:
{
"dependencies"
:
{
"graphql"
:
"^0.9.1"
,
"graphql"
:
"^0.9.3"
,
"isomorphic-fetch"
:
"^2.2.1"
,
"next"
:
"latest"
,
"next"
:
"latest"
,
"react"
:
"^15.4.2"
,
"prop-types"
:
"^15.5.8"
,
"react-apollo"
:
"^1.0.0-rc.2"
,
"react"
:
"^15.5.4"
,
"react-apollo"
:
"^1.1.3"
,
"react-dom"
:
"^15.5.4"
,
"redux"
:
"^3.6.0"
"redux"
:
"^3.6.0"
},
},
"author"
:
""
,
"author"
:
""
,
...
...
examples/with-apollo/lib/init
Client
.js
→
examples/with-apollo/lib/init
Apollo
.js
View file @
f4d6cbfc
import
{
ApolloClient
,
createNetworkInterface
}
from
'react-apollo'
import
{
ApolloClient
,
createNetworkInterface
}
from
'react-apollo'
import
fetch
from
'isomorphic-fetch'
let
apolloClient
=
null
let
apolloClient
=
null
function
_initClient
(
headers
,
initialState
)
{
// Polyfill fetch() on the server (used by apollo-client)
if
(
!
process
.
browser
)
{
global
.
fetch
=
fetch
}
function
create
(
initialState
)
{
return
new
ApolloClient
({
return
new
ApolloClient
({
initialState
,
initialState
,
ssrMode
:
!
process
.
browser
,
ssrMode
:
!
process
.
browser
,
// Disables forceFetch on the server (so queries are only run once)
dataIdFromObject
:
result
=>
result
.
id
||
null
,
networkInterface
:
createNetworkInterface
({
networkInterface
:
createNetworkInterface
({
uri
:
'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn'
,
uri
:
'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn'
,
// Server URL (must be absolute)
opts
:
{
opts
:
{
// Additional fetch() options like `credentials` or `headers`
credentials
:
'same-origin'
credentials
:
'same-origin'
// Pass headers here if your graphql server requires them
}
}
})
})
})
})
}
}
export
const
initClient
=
(
headers
,
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
)
{
if
(
!
process
.
browser
)
{
return
_initClient
(
headers
,
initialState
)
return
create
(
initialState
)
}
}
// Reuse client on the client-side
if
(
!
apolloClient
)
{
if
(
!
apolloClient
)
{
apolloClient
=
_initClient
(
headers
,
initialState
)
apolloClient
=
create
(
initialState
)
}
}
return
apolloClient
return
apolloClient
}
}
examples/with-apollo/lib/withData.js
View file @
f4d6cbfc
import
'isomorphic-fetch'
import
React
from
'react'
import
React
from
'react'
import
PropTypes
from
'prop-types'
import
{
ApolloProvider
,
getDataFromTree
}
from
'react-apollo'
import
{
ApolloProvider
,
getDataFromTree
}
from
'react-apollo'
import
{
initClient
}
from
'./initClient'
import
initApollo
from
'./initApollo'
export
default
ComposedComponent
=>
{
return
class
WithData
extends
React
.
Component
{
static
displayName
=
`WithData(
${
ComposedComponent
.
displayName
}
)`
static
propTypes
=
{
serverState
:
PropTypes
.
object
.
isRequired
}
export
default
(
Component
)
=>
(
class
extends
React
.
Component
{
static
async
getInitialProps
(
ctx
)
{
static
async
getInitialProps
(
ctx
)
{
const
headers
=
ctx
.
req
?
ctx
.
req
.
headers
:
{}
let
serverState
=
{}
const
client
=
initClient
(
headers
)
const
props
=
{
// Evaluate the composed component's getInitialProps()
url
:
{
query
:
ctx
.
query
,
pathname
:
ctx
.
pathname
},
let
composedInitialProps
=
{}
...
await
(
Component
.
getInitialProps
?
Component
.
getInitialProps
(
ctx
)
:
{})
if
(
ComposedComponent
.
getInitialProps
)
{
composedInitialProps
=
await
ComposedComponent
.
getInitialProps
(
ctx
)
}
}
// Run all graphql queries in the component tree
// and extract the resulting data
if
(
!
process
.
browser
)
{
if
(
!
process
.
browser
)
{
const
apollo
=
initApollo
()
// Provide the `url` prop data in case a graphql query uses it
const
url
=
{
query
:
ctx
.
query
,
pathname
:
ctx
.
pathname
}
// Run all graphql queries
const
app
=
(
const
app
=
(
<
ApolloProvider
client
=
{
client
}
>
<
ApolloProvider
client
=
{
apollo
}
>
<
Compo
nent
{...
p
rops
}
/
>
<
Compo
sedComponent
url
=
{
url
}
{...
composedInitialP
rops
}
/
>
<
/ApolloProvider
>
<
/ApolloProvider
>
)
)
await
getDataFromTree
(
app
)
await
getDataFromTree
(
app
)
// Extract query data from the Apollo's store
const
state
=
apollo
.
getInitialState
()
serverState
=
{
apollo
:
{
// Make sure to only include Apollo's data state
data
:
state
.
data
}
}
}
}
return
{
return
{
initialState
:
{
serverState
,
apollo
:
{
...
composedInitialProps
data
:
client
.
getInitialState
().
data
}
},
headers
,
...
props
}
}
}
}
constructor
(
props
)
{
constructor
(
props
)
{
super
(
props
)
super
(
props
)
this
.
client
=
initClient
(
this
.
props
.
headers
,
this
.
props
.
initial
State
)
this
.
apollo
=
initApollo
(
this
.
props
.
server
State
)
}
}
render
()
{
render
()
{
return
(
return
(
<
ApolloProvider
client
=
{
this
.
client
}
>
<
ApolloProvider
client
=
{
this
.
apollo
}
>
<
Component
{...
this
.
props
}
/
>
<
Compo
sedCompo
nent
{...
this
.
props
}
/
>
<
/ApolloProvider
>
<
/ApolloProvider
>
)
)
}
}
}
}
)
}
examples/with-apollo/package.json
View file @
f4d6cbfc
{
{
"name"
:
"with-apollo"
,
"name"
:
"with-apollo"
,
"version"
:
"
1.0.1
"
,
"version"
:
"
2.0.0
"
,
"scripts"
:
{
"scripts"
:
{
"dev"
:
"next"
,
"dev"
:
"next"
,
"build"
:
"next build"
,
"build"
:
"next build"
,
"start"
:
"next start"
"start"
:
"next start"
},
},
"dependencies"
:
{
"dependencies"
:
{
"graphql"
:
"^0.9.1"
,
"graphql"
:
"^0.9.3"
,
"isomorphic-fetch"
:
"^2.2.1"
,
"next"
:
"latest"
,
"next"
:
"latest"
,
"react"
:
"^15.4.2"
,
"prop-types"
:
"^15.5.8"
,
"react-apollo"
:
"^1.0.0-rc.3"
"react"
:
"^15.5.4"
,
"react-dom"
:
"^15.5.4"
,
"react-apollo"
:
"^1.1.3"
},
},
"author"
:
""
,
"author"
:
""
,
"license"
:
"ISC"
"license"
:
"ISC"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment