adamsoffer / next-apollo

React higher-order component for integrating Apollo Client with Next.js
MIT License
481 stars 64 forks source link

Upgrade apollo-common to ^3.0.1 and move @apollo packages to peerDependencies #52

Closed MrOrz closed 5 years ago

MrOrz commented 5 years ago

Apollo hooks has gone through its beta process a few days ago. This PR updates the dependency to @apollo/react-common to reflect the update.

This PR also moves @apollo/react-common, @apollo/react-ssr and apollo-client to peer dependency to make sure that when installed, there is only one copy of Apollo client. If multiple copy is installed, such error will occur:

Invariant Violation: Could not find "client" in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.
    at new InvariantError (/projects/next-apollo-example/node_modules/ts-invariant/lib/invariant.js:16:28)
    at invariant (/projects/next-apollo-example/node_modules/ts-invariant/lib/invariant.js:28:15)
    at QueryData.OperationData.refreshClient (/projects/next-apollo-example/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:56:115)
    at QueryData.execute (/projects/next-apollo-example/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:115:10)
    at /projects/next-apollo-example/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:480:55
    at useDeepMemo (/projects/next-apollo-example/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:436:14)
    at useBaseQuery (/projects/next-apollo-example/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:479:16)
    at useQuery (/projects/next-apollo-example/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:496:10)
    at PostList (/projects/next-apollo-example/.next/server/static/development/pages/index.js:325:87)
    at processChild (/projects/next-apollo-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:2888:14)
    at resolve (/projects/next-apollo-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:2812:5)
    at ReactDOMServerRenderer.render (/projects/next-apollo-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3202:22)
    at ReactDOMServerRenderer.read (/projects/next-apollo-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3161:29)
    at renderToString (/projects/next-apollo-example/node_modules/react-dom/cjs/react-dom-server.node.development.js:3646:27)
    at render (/projects/next-apollo-example/node_modules/next-server/dist/server/render.js:81:16)

The root cause of such error is, when there are two copy of @apollo/react-common, withData will instantiate React context under module scope of node_modules/next-apollo/node_modules/@apollo/react-common/lib/context/ApolloContext.js, while useQuery in the project will look up the context in node_modules/@apollo/react-common/lib/context/ApolloContext.js. This PR moves them to peerDependency to avoid this quirk. As a trade-off, the user must install these libraries manually along side with next-apollo.

adamsoffer commented 5 years ago

Looks great! Thanks so much for the PR @MrOrz !