question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

3.0 Caching: When you don't request the fields you have specified as keyFields

See original GitHub issue

Hey,

  • I have created a typePolicy for an entity in which I use keyFields with nested fields.
  • In a query, I am requesting the entity, but I am not requesting the fields that I have specified in the typePolicy.

Intended outcome:

  • I can still get the results of my query.
  • While I can see that this is a very useful feature (it is talked about at this timestamp here https://youtu.be/n_j8QckQN5I?t=1318), I would like to be able to override it. The vibe that I get from the video is that this should be a “warning”, not an error, but maybe I am just doing something wrong.

Actual outcome:

  • I can’t get the results of my query, I get a networkStatus of 8 and Network error: Missing field state while computing key fields

In the next slide in the presentation(https://youtu.be/n_j8QckQN5I?t=1381), there is a solution for this issue – setting keyFields: false on the SearchResults typePolicy, but I don’t see how I can apply it my code without really, really complicating things. I feel like what I need is to be able to nest typePolicies (??? I really feel lost though, this is just a guess.)

Like this for example:

typePolicies: {
  Query: {
     fields: {
       theQueryThatIsSadlyErroring: {
         fields: {
           theEntityThatIAmNotRequestingAllTheKeyingFieldsFor: {
             keyFields: false
           }
         }
       }
     }
  }
}

To me the above code would mean, “don’t normalise entities when they come as part of this specific query”.

Any help or direction is extremely appreciated, thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
haffmaestrocommented, Nov 4, 2020

What’s holding you back from requesting the fields that are needed for computing the ID of the object?

Hundreds of queries 😦

In my case the field I need for keyFields is the same for every Node resource, is there a way to configure Apollo to always add this field, like __typename?

2reactions
benjamncommented, Dec 19, 2019

I would strongly recommend requesting any fields that are involved in keyFields, even if you never use those fields explicitly in your own code.

To make that simpler, you could use a fragment:

query {
  someRootQueryField {
    ...PlatonicStateIdFragment
    apple {
      ...PlatonicStateIdFragment
      state {
        title
      }
    }
    banana {
      ...PlatonicStateIdFragment
    }
  }
}

fragment PlatonicStateIdFragment on AppleBanana {
  platonic { id }
  state { id }
}

Given the above query, your typePolicies example code looks fine to me.

If your query omits information that’s used by keyFields, the cache won’t know how to compute an ID for the object, and may throw an exception. Passing keyFields: false prevents the cache from trying to compute an ID, but it also means the object will be embedded directly within its parent object, so you won’t be able to benefit from getting new information about that object from different queries over time. Note that keyFields only works for objects that have __typename fields, not for individual fields within objects.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Previewing the Apollo Client 3 Cache - Apollo GraphQL Blog
Apollo Client's caching system is core to both the user experience ... You can now specify which fields of an object represent its...
Read more >
Apollo client v3 "Cache data may be lost when replacing the ...
By default, the cache stores a separate value for every unique combination of argument values you provide when querying a particular field.
Read more >
Configuring the Cache – Angular - GraphQL Code Generator
In particular, you can: Specify custom primary key fields; Customize the storage and retrieval of individual fields; Customize the ...
Read more >
Values that you specify when you create or update a distribution
When you use the CloudFront console to create a new distribution or update ... only when you choose Whitelist for Cache Based on...
Read more >
Customizing the behavior of cached fields - Client (React)
You can customize how a particular field in your Apollo Client cache is read ... Perhaps no keyFields were specified for the Author...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found