question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use apollo client without/outside component?

See original GitHub issue

I have to use apollo client without react component , it means that I cannot use useApolloClient hook and withApollo HOC. I wanna use client.query and client.mutate methods, so I have to be able to access client instance. In this situation, I extracted client instance as separate module and import that instance. Is it right way to use apollo client outside component? If it isn’t, what is the best approach?

client.ts (NOT REACT COMPONET)

export const client = new ApolloClient(...)

outsideComponent.ts (NOT REACT COMPONET)

import { client } from './client';

const veryComplexQuery = client.query....

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
naishecommented, Nov 21, 2020

This is what I have done. I am using a singleton pattern.

let apolloClient: ApolloClient<NormalizedCacheObject> | undefined;

export const getApolloClient = () => {
  if (apolloClient) {
    return apolloClient;
  }
 // -- code to configure client is redacted -- 
  apolloClient = new ApolloClient({
    // ...
    // various options
  });

  return apolloClient;
};

Now, wherever I need client, I call getApolloClient().

1reaction
Natas0007commented, Sep 17, 2020

Hey @baeharam , do you mind providing an example of how you solved this with dependency injection? Are you referring to your initial explanation of creating the client instance in a separate module and then just importing it where you need it? Have you ran into any issues with that config? Thx for your time!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run Queries/mutation outside of react components - Help
Hi Everyone, I have js file with multiple helper function which i call and use from many different components. Currently i call an...
Read more >
How to call GraphQL outside a component - Stack Overflow
From a React component you can use useLazyQuery from Apollo Client which will only fire when you call it.
Read more >
How to use apollo client without hooks (useQuery, and ...
In this article you will find out how to use apollo client without hooks such as useQuery or useMutation.
Read more >
Components - Client (React) - Apollo GraphQL Docs
A guide to using the Apollo GraphQL Client with React.
Read more >
How to: Vue-apollo v4 useQuery, outside Vue-component ...
Vue- apollo version 4 brought us composition-api that we can use with VueJS 3. It is a great library. But how do you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found