简体   繁体   中英

Apollo Client Query Deduplication on Cached Object

I recently read an article about Apollo Client Caching on the official blog , with a screenshot here.

According to the article, if the current query contains a cached object, along with other objects, the query will be deduplicated to only query the rest of the objects.

However, I did some testing by logging out queries on the server, which suggested that the query had not been partially deduplicated. Instead, the entire query got sent to the server.

Can anyone provide any insight on this. Thanks very much.

Test :

First query:

{
   post(_id: 1) {
      _id
      title
   }
}

Second query:

{
   post(_id: 1) {
      _id
      title
   }
   author(_id: 1) {
      _id
      firstName
   }
}

Intended outcome: The secomd query received by the server only contains

author(_id: 1) {
   _id
   firstName
}

since post(_id: 1) has been cached after the first query is sent, according to the blog.

Actual outcome: Server log: (the second query has NOT been deduplicated)

{
   "operationName": null,
   "variables": {},
   "query": "{\n  post(_id: 1) {\n    _id\n    title\n    __typename\n  
   }\n}\n"
} /graphql 200 81 - 0.520 ms

{
   "operationName": null,
   "variables": {},
   "query": "{\n  post(_id: 1) {\n    _id\n    title\n    __typename\n  
   }\n  author(_id: 1) {\n    _id\n    firstName\n    __typename\n  }\n}\n"
} /graphql 200 140 - 0.726 ms

There is a subject called Query deduplication in Apollo-Client
that comes from Apollo-Link (the transport layer of Apollo-Client )
but what it does is deduplicate existing queries that are currently being fetched

Query deduplication can be useful if many components display the same data, but you don't want to fetch that data from the server many times. It works by comparing a query to all queries 进行中的所有查询进行比较来工作

you can read more about it here and here

But it doesn't relate so much to the cache..
I think what you're looking for is a better understanding of how cache is managed in Apollo-Client

So how is caching handled in Apollo-Client ?
you can find a more through article about it here from their official docs: apollo-client-caching

more specifically to your case i believe that if you would use watchQuery / query and make sure the fetchPolicy is cache-first then it might not resend the query

I hope this gives you better insight on caching in Apollo-Client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM