Unverified Commit c8e5f925 authored by Roman Ernst's avatar Roman Ernst Committed by GitHub

Make apollo HOC composable (#10422)

* Wraps config in higher-order function
parent 4203d7fd
...@@ -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)
......
...@@ -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,
})
...@@ -26,4 +26,4 @@ const IndexPage = props => ( ...@@ -26,4 +26,4 @@ const IndexPage = props => (
</App> </App>
) )
export default withApollo(IndexPage) export default withApollo()(IndexPage)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment