Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo Client what are active queries?

My Setup

  • Typescript React App
  • Backend GraphQL API
  • Apollo Client
  • Inspecting with Apollo Client Devtools Extension

My question

  • What are active queries? The Apollo Docs talks about active queries a lot but I am struggling to get an actual definition.
  • I understood that an active query is basically a link from your component state to the overall global state of the Apollo Client cache. Is this correct?
  • Why do I see 2 queries being listed when I only have one query in my react component (see screen recording). For example in my events pages I call on query listEvents but then I see that query appear twice in my active queries when I load the page. Is seems to be the same for any new query.
  • Why do my active queries keep increasing when I am just navigating back and forth between the same pages and fetching the same data. Based on my understanding, I would expect the number of active queries not to increase in this case as I am not introducing and new queries.
  • I am worried that there is some issue in my setup leading to new active queries being created instead of 'recycling' existing ones. Am I right? And if so how can I debug/fix this?

Active Queries Screen Recording

Thanks,

like image 741
Simon Verhoeven Avatar asked Apr 16 '21 12:04

Simon Verhoeven


People also ask

Does Apollo Client batch queries?

If you use BatchHttpLink instead of HttpLink as your terminating link, Apollo Client automatically batches executed GraphQL operations and transmits them to your server according to the batching options you provide.

How do I clear the Apollo Client cache?

Resetting the cache This method is asynchronous, because it also refetches any of your active queries. To reset the cache without refetching active queries, use client. clearStore() instead of client. resetStore() .

Does Apollo Client use Fetch?

A fetch policy defines how Apollo Client uses the cache for a particular query. The default policy is cache-first , which means Apollo Client checks the cache to see if the result is present before making a network request. If the result is present, no network request occurs.


Video Answer


1 Answers

Active queries are queries running in mounted components so Apollo knows what query result to update when cache is updated. If you're sure you're not running those queries at the same time, you can check your routing library to see if they don't keep your last page in cache.

like image 73
Emmanuel Tabard Avatar answered Oct 19 '22 10:10

Emmanuel Tabard