3.0 Caching: When you don't request the fields you have specified as keyFields
See original GitHub issueHey,
- 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:
- Created 4 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hundreds of queries 😦
In my case the
field
I need forkeyFields
is the same for every Node resource, is there a way to configure Apollo to always add this field, like__typename
?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:
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. PassingkeyFields: 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 thatkeyFields
only works for objects that have__typename
fields, not for individual fields within objects.