0

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

Giles Bradshaw
  • 1,249
  • 2
  • 9
  • 8

1 Answers1

0

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.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Thank you @daniel - I'm actually trying to do in real app is the reverse ie have apollo provider and hook as peer dependencies. But you pointed me in the right direction. What I have found is that by removing the resolve and commonjs plugins I can have the dependencies in development dependencies and yet still peer dependencies during development - the modules from the main app are used by the package see here https://github.com/gilesbradshaw/use-query/pull/1 – Giles Bradshaw Jan 16 '20 at 20:16
  • ps actually it's not quite as I thought - the main app still uses the packages node modules if they are there. So I am having to delete the development ones in order to use it.. – Giles Bradshaw Jan 16 '20 at 21:42