From f6a580867a41d5120663281105f66c3b1d310ec7 Mon Sep 17 00:00:00 2001 From: pex Date: Tue, 4 Feb 2020 23:38:48 +0100 Subject: [PATCH] Make apollo HOC composable * Wraps config in higher-order function --- examples/with-apollo/lib/apollo.js | 9 +++------ examples/with-apollo/pages/client-only.js | 6 ++---- examples/with-apollo/pages/index.js | 2 +- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/examples/with-apollo/lib/apollo.js b/examples/with-apollo/lib/apollo.js index 54e4e3b8c8a4c22..f6532128069e241 100644 --- a/examples/with-apollo/lib/apollo.js +++ b/examples/with-apollo/lib/apollo.js @@ -12,11 +12,8 @@ let globalApolloClient = null * Creates and provides the apolloContext * to a next.js PageTree. Use it by wrapping * 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 client = apolloClient || initApolloClient(apolloState) return ( @@ -104,7 +101,7 @@ export function withApollo(PageComponent, { ssr = true } = {}) { * Creates or reuses apollo client in the browser. * @param {Object} initialState */ -function initApolloClient(initialState) { +const initApolloClient = (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 (typeof window === 'undefined') { @@ -123,7 +120,7 @@ function initApolloClient(initialState) { * Creates and configures the ApolloClient * @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 return new ApolloClient({ ssrMode: typeof window === 'undefined', // Disables forceFetch on the server (so queries are only run once) diff --git a/examples/with-apollo/pages/client-only.js b/examples/with-apollo/pages/client-only.js index 1866c0941bf5256..7fa37e3ca22b520 100644 --- a/examples/with-apollo/pages/client-only.js +++ b/examples/with-apollo/pages/client-only.js @@ -26,7 +26,5 @@ const ClientOnlyPage = props => ( ) -export default withApollo(ClientOnlyPage, { - // Disable apollo ssr fetching in favour of automatic static optimization - ssr: false, -}) +// Disable apollo ssr fetching in favour of automatic static optimization +export default withApollo({ ssr: false })(ClientOnlyPage) diff --git a/examples/with-apollo/pages/index.js b/examples/with-apollo/pages/index.js index fd7361809593d79..d40059d1a4620c2 100644 --- a/examples/with-apollo/pages/index.js +++ b/examples/with-apollo/pages/index.js @@ -26,4 +26,4 @@ const IndexPage = props => ( ) -export default withApollo(IndexPage) +export default withApollo()(IndexPage)