0

I need to find out if Apollo has data cached before trying to call a query for that data from the cache.

This code throws the "Invariant Violation: Can't find field ...... on object" error if we haven't already called the USER_QUERY. However I don't have a way to check if we've ever called it.

const cachedData = client.readQuery({
   query: USER_QUERY,
   variables: queryVariables,
});

My problem is I don't know how to check if we've cached data or not before calling any cache.

1 Answers1

1

you can wrap the client.readQuery portion with try catch as suggest here and here ;)

try {
  const cachedData = client.readQuery({
     query: USER_QUERY,
     variables: queryVariables,
  });
} catch(e) {
  // maybe you can safely swallow the error
}
Yichaoz
  • 7,886
  • 8
  • 45
  • 83