Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide default empty StoreObject for root IDs like ROOT_QUERY. #7100

Merged
merged 2 commits into from
Sep 30, 2020

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Sep 29, 2020

Note: this PR builds on #7098.

This change means the existence of root objects like ROOT_QUERY and ROOT_MUTATION in the InMemoryCache will no longer depend on whether other queries/mutations have previously written data into the cache.

This matters because (until just recently: #7098) cache.read would return null for a completely missing ROOT_QUERY object, but throw missing field errors if ROOT_QUERY existed but did not have the requested fields:

const cache = new InMemoryCache(); // empty
const query = gql`query { some fields }`;

// Returns null, as before:
const result = cache.readQuery({ query });

// Before PR #7098, this write would cause the ROOT_QUERY object
// to be created, changing the behavior of cache.readQuery below.
cache.writeQuery({
  query: gql`query { unrelated fields }`,
  data: {...},
});

// Previously threw a missing field error, but now returns null:
const result = cache.readQuery({
  query: gql`query { some fields }`,
});

In other words, this commit hides the difference between the absence of ROOT_QUERY and its incompleteness, so unrelated cache writes can no longer unexpectedly influence the null-returning vs. exception-throwing behavior of cache.read.

As an additional motivation, with AC3 field policies, it's possible now to define synthetic root query fields that have a chance of returning useful values even if the cache is completely empty. Providing a default empty object for root IDs like ROOT_QUERY is important to let those read functions be called, instead of prematurely returning null from cache.read when ROOT_QUERY does not exist.

Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM - thanks @benjamn!

@benjamn benjamn force-pushed the default-StoreObject-for-root-IDs branch from 6c87c20 to 63c7cb0 Compare September 30, 2020 18:22
Base automatically changed from fr1-prevent-cache.read-from-throwing to release-3.3 September 30, 2020 20:52
This change means the existence of root objects like ROOT_QUERY and
ROOT_MUTATION will no longer depend on whether other queries/mutations
have previously written data into the cache.

This matters because (until just recently) cache.read would return null
for a completely missing ROOT_QUERY object, but throw missing field errors
if ROOT_QUERY existed but did not have the requested fields.

In other words, this commit hides the difference between the absence of
ROOT_QUERY and its incompleteness, so unrelated cache writes can no longer
unexpectedly influence the null-returning vs. exception-throwing behavior
of cache.read.

As an additional motivation, with AC3 field policies, it's possible now to
define synthetic root query fields that have a chance of returning useful
values even if the cache is completely empty. Providing a default empty
object for root IDs like ROOT_QUERY is important to let those read
functions be called, instead of prematurely returning null from cache.read
when ROOT_QUERY does not exist.

Note: this PR builds on PR #7098.
@benjamn benjamn force-pushed the release-3.3 branch 2 times, most recently from 528a97f to e387014 Compare September 30, 2020 21:08
@benjamn benjamn force-pushed the default-StoreObject-for-root-IDs branch from 63c7cb0 to cf3c2a4 Compare September 30, 2020 21:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants