12

This must be user error, but I've got an app with a simple currentUser query that looks at a JWT for an id, looks it up, and returns the appropriate user.

I can look at devtools and see that it's in the cache as __ref:User:19

export const CURRENT_USER_QUERY = gql`
  query{
    currentUser {
      id
      fullName
      email
    }
  }
`

But in my component, when I do const { currentUser } = client.readQuery({ query: CURRENT_USER_QUERY }); it (intermittently) blows up because:

Cannot destructure property 'currentUser' of 'client.readQuery(...)' as it is null.

User:19 exists and is all the stuff I need. I hit reload, and that same page works.

I have my default error policy set to "all" in the client.

This is version 3.3.11 of the client. Chrome v88.04, for what that's worth. React 17.01.

Am I missing something here? Should that not return a value synchronously (and dependably) if that item's in the cache?

Is there a better way to deal with that situation? I'm trying to move this app away from storing things in redux or some context provider since it's already being handled by Apollo. Seems redundant to have that responsibility handled by a bunch of different things.

CrustyRatFink
  • 464
  • 1
  • 5
  • 15
  • The idea is right, just watch out for any race condition that might be happening depending on where you read the original data in using `useQuery` – Vandesh Mar 23 '21 at 09:59

4 Answers4

6

I was facing issues with .readQuery() last night. I was getting null returned everytime, though the logic was right. I was calling .readQuery() within a component I imported into my React page.

What ended up being my issue is that I was not updating the same query I made in the "parent" react page as the one in the component.

I don't know if this is the same problem you're running into, but I thought I'd leave this here for perpetuity and perspective.

allo
  • 3,955
  • 8
  • 40
  • 71
Anthony Peña
  • 171
  • 1
  • 7
  • 2
    Indeed, you need to use the same query, else the cache won't find it. Apparently, that was my problem too, thx! – DBencz Oct 27 '21 at 15:57
4

I was facing the same issue. I fixed it like this:

const existingData = cache.readQuery({
   query: GET_CONTRACT,
   variables: {
       ...variables, // fixed: passing the same reference to variables
   },
});
Community
  • 1
  • 1
Alia Anis
  • 1,355
  • 11
  • 7
1

The problem here can be in new variables or absence of variables. So the query was made with variables and you try to get it from the cache without. .readQuery from cache must be identical

0

You need to pass the same variable with the same values you use when executing the mutation and in the same order

existingData = cache.readQuery({
   query: QUERY_NAME,
     variables: {
        x: mutationValue1,
        y: mutationValue2
        z: mutationValue3
   },
 });`
yaxx
  • 529
  • 1
  • 6
  • 22