Search code examples
react-apollorollupjsreact-apollo-hooks

bundling apollo useQuery hook with rollup


I'm trying to create a package that exports functionality using useQuery

However I get the following error:

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.

This is even with simplest possible example which just exports useQuery (obvs real code does more than this)

I know my code is ok otherwise as if I import useQuery using

import { useQuery } from '@apollo/react-hooks'

it works fine

simple example is here..

https://github.com/gilesbradshaw/use-query


Solution

  • The hook and the ApolloProvider used should be from the same module, otherwise the context used by the hook will be different than what is provided by the ApolloProvider. You should export ApolloProvider in your package in addition to the hook, and then make sure you import it from your package whereever you're using the hook.