How to get access to `client` object anywhere in the app with Apollo Client?

Viewed 1570

I have a basic app using Apollo Client v1:

const client = new ApolloClient({...

...

<ApolloProvider client={client}>
   <App />
</ApolloProvider>,

i was wondering how i could give the rest of my app access to the client object so that they can make queries using client.query?

Is the only way to export client and then import it in another file?

Since my app is wrapped in a ApolloProvider, I figured there might be a way of importing client using an Apollo specific import?

1 Answers
import { useApolloClient } from '@apollo/client';

function SomeComponent() {
  const client = useApolloClient();
  // `client` is now set to the `ApolloClient` instance being used by the
  // application (that was configured using something like `ApolloProvider`)
}
Related