Skip to content

Commit

Permalink
Make apollo HOC composable
Browse files Browse the repository at this point in the history
* Wraps config in higher-order function
  • Loading branch information
pex committed Feb 4, 2020
1 parent ec39aa4 commit f6a5808
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
9 changes: 3 additions & 6 deletions examples/with-apollo/lib/apollo.js
Expand Up @@ -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 (
Expand Down Expand Up @@ -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') {
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions examples/with-apollo/pages/client-only.js
Expand Up @@ -26,7 +26,5 @@ const ClientOnlyPage = props => (
</App>
)

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)
2 changes: 1 addition & 1 deletion examples/with-apollo/pages/index.js
Expand Up @@ -26,4 +26,4 @@ const IndexPage = props => (
</App>
)

export default withApollo(IndexPage)
export default withApollo()(IndexPage)

0 comments on commit f6a5808

Please sign in to comment.