What is the difference between client.query and useQuery in Apollo?
Asked Answered
L

0

16

In my query, I use a blend of remote and local properties. The local properties have different values depending on:

  1. The remote properties
  2. The current unix timestamp

The local resolvers computes the local state according to these rules. Now, I face a scenario whereby I need to forego useQuery and use Apollo Client's query for enhanced flexibility.

Other than structural differences (e.g. useQuery accepts two parameters, whereas client.query takes only one parameter - the options objects), is it safe to use one over the other?

I identified at least one difference not described in the docs. When I call client.query multiple times, the local resolvers are run only once, at the beginning. With useQuery, the data always gets recomputed and it has different values (as per #2 above).

Leukemia answered 2/11, 2019 at 18:26 Comment(3)
useQuery, like the Query component and the graphql HOC, uses watchQuery under the hood. query just runs your query and returns the result, while watchQuery also subscribes to subsequent changes to the cache.Divulsion
Thanks @DanielRearden, but I don't think this explains the difference I found. If the cache is indeed updated after running client.query, when I call it the 2nd time it should reflect the cached modified by the resolvers. I understand why watchQuery syncs this correctly, but I don't understand why client.query doesn't run the resolvers, even if I call it on an interval basis.Leukemia
Unfortunately, I'm not familiar enough with how the more recent versions of apollo-client handle local state internally to give a full answer. Just hoping to nudge you onto the right track.Divulsion

© 2022 - 2024 — McMap. All rights reserved.