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
c8e5f925
Unverified
Commit
c8e5f925
authored
Feb 05, 2020
by
Roman Ernst
Committed by
GitHub
Feb 05, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make apollo HOC composable (#10422)
* Wraps config in higher-order function
parent
4203d7fd
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
6 additions
and
11 deletions
+6
-11
apollo.js
examples/with-apollo/lib/apollo.js
+3
-6
client-only.js
examples/with-apollo/pages/client-only.js
+2
-4
index.js
examples/with-apollo/pages/index.js
+1
-1
No files found.
examples/with-apollo/lib/apollo.js
View file @
c8e5f925
...
@@ -12,11 +12,8 @@ let globalApolloClient = null
...
@@ -12,11 +12,8 @@ let globalApolloClient = null
* Creates and provides the apolloContext
* Creates and provides the apolloContext
* to a next.js PageTree. Use it by wrapping
* to a next.js PageTree. Use it by wrapping
* your PageComponent via HOC pattern.
* your PageComponent via HOC pattern.
* @param {Function|Class} PageComponent
* @param {Object} [config]
* @param {Boolean} [config.ssr=true]
*/
*/
export
function
withApollo
(
PageComponent
,
{
ssr
=
true
}
=
{})
{
export
const
withApollo
=
({
ssr
=
true
}
=
{})
=>
PageComponent
=>
{
const
WithApollo
=
({
apolloClient
,
apolloState
,
...
pageProps
})
=>
{
const
WithApollo
=
({
apolloClient
,
apolloState
,
...
pageProps
})
=>
{
const
client
=
apolloClient
||
initApolloClient
(
apolloState
)
const
client
=
apolloClient
||
initApolloClient
(
apolloState
)
return
(
return
(
...
@@ -104,7 +101,7 @@ export function withApollo(PageComponent, { ssr = true } = {}) {
...
@@ -104,7 +101,7 @@ export function withApollo(PageComponent, { ssr = true } = {}) {
* Creates or reuses apollo client in the browser.
* Creates or reuses apollo client in the browser.
* @param {Object} initialState
* @param {Object} initialState
*/
*/
function
initApolloClient
(
initialState
)
{
const
initApolloClient
=
initialState
=>
{
// Make sure to create a new client for every server-side request so that data
// Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad)
// isn't shared between connections (which would be bad)
if
(
typeof
window
===
'undefined'
)
{
if
(
typeof
window
===
'undefined'
)
{
...
@@ -123,7 +120,7 @@ function initApolloClient(initialState) {
...
@@ -123,7 +120,7 @@ function initApolloClient(initialState) {
* Creates and configures the ApolloClient
* Creates and configures the ApolloClient
* @param {Object} [initialState={}]
* @param {Object} [initialState={}]
*/
*/
function
createApolloClient
(
initialState
=
{})
{
const
createApolloClient
=
(
initialState
=
{})
=>
{
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
// Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
return
new
ApolloClient
({
return
new
ApolloClient
({
ssrMode
:
typeof
window
===
'undefined'
,
// Disables forceFetch on the server (so queries are only run once)
ssrMode
:
typeof
window
===
'undefined'
,
// Disables forceFetch on the server (so queries are only run once)
...
...
examples/with-apollo/pages/client-only.js
View file @
c8e5f925
...
@@ -26,7 +26,5 @@ const ClientOnlyPage = props => (
...
@@ -26,7 +26,5 @@ const ClientOnlyPage = props => (
</App>
</App>
)
)
export default withApollo(ClientOnlyPage, {
// Disable apollo ssr fetching in favour of automatic static optimization
// Disable apollo ssr fetching in favour of automatic static optimization
export default withApollo({ ssr: false })(ClientOnlyPage)
ssr: false,
})
examples/with-apollo/pages/index.js
View file @
c8e5f925
...
@@ -26,4 +26,4 @@ const IndexPage = props => (
...
@@ -26,4 +26,4 @@ const IndexPage = props => (
<
/App
>
<
/App
>
)
)
export
default
withApollo
(
IndexPage
)
export
default
withApollo
(
)(
IndexPage
)
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