cache.readFragment only returning __typename

Hi, new to apollo and really struggling to understand some of the things I am seeing.

I have this query:

    query {
        getUserCollectionListing {
            edges {
                node {
                    id
                    owner
                    name
                    items {
                        ... on object_Metadata {
                            id
                            __typename
                        }
                    }
                    __typename
                }
            }
        }
    }

Which is used like so:

    const { loading, error, data, refetch } = useQuery(GET_USER_BASKETS, {
        fetchPolicy: 'network-only'
    });

I am trying to access the cache for this query using ‘readFragment’:

        cache.readFragment({
            id: currentBasket, // looks like: object_UserCollection:822134
            
            fragment: gql`
                fragment Object_UserCollectionFragment on Object_UserCollection {
                id
                name
                owner
                items
                __typename
                }
            `,
            // fragmentName: 'Object_UserCollectionFragment', // tried with and without
        });

But the problem is the object returned by ‘readFragment’ is not complete, and only contains the ‘__typename’:
image

I feel like I have circled the documentation and examples endlessly and I don’t understand why this would be the case. What would cause the rest of that entry to be missing?

‘cache.readQuery’ on 'GET_USER_BASKETS` appears to returned the cached data, so I can get it from there if needed, but I would really like to understand the behavior I’m seeing.

Thanks in advance.

Hey @abrenoch :wave:

Can you check to see what the contents of your cache looks like? You can dump the cache to the console using the client.extract() or client.cache.extract() function.

console.log(client.extract())

You should see a record with key object_UserCollection:822134 in there. Can you verify the data you expect for that record is available in the cache?

Hey there, really appreciate the fast response!

I can confirm the items exist in the cache as expected:

Maybe something with how the fragment is formatted? I feel like there has to be something simple or obvious I’m overlooking.

Something I’m noticing that might be the problem here is that it looks like items might be an array and your fragment has no subselection on that field. See if adding a subselection fixes it.

fragment Object_UserCollectionFragment on Object_UserCollection {
  # ...
  items {
    __typename
  }
}

I like the idea, but unfortunately didnt seem to help. I also tried omitting the ‘items’ property entirely to no avail.

Bummer! As a debugging measure, perhaps add one field at a time to the fragment to see if you’re able to get any data from any of the fields. If you’re still not successful with even a single field, you might consider opening an issue with a reproduction for us to look at.

Oh man, I just found the problem :clown_face:

‘Object_UserCollection’ should have been ‘object_UserCollection’ in my fragment. Correcting it there fixed my issue.

I spent too long on that, how embarrassing lol. Maybe enough for tonight :smile:

Thank you for your prompt replies, I appreciate it more than you know!

1 Like

I always say the hardest bugs to solve require the smallest code changes :rofl:. I can’t tell you how many times this has happened to me! Glad you found the issue!