Skip to content

Commit

Permalink
Stop treating dangling references differently from empty objects. (#6365
Browse files Browse the repository at this point in the history
)

Question: When cache.read{Query,Fragment} encounters a dangling reference
(one that does not currently refer to any normalized entity object in the
cache), should the cache behave as if the (nonexistent) target of the
reference was an empty object, and report any requested fields as missing,
or should the cache generate some other kind of error that reflects the
the absence of the whole object?

I think we could answer this question either way, but I'm leaning towards
the first option.

Note: it's normal for the cache to end up with dangling references when
whole entity objects are evicted, or when a reference is created (e.g. by
toReference) without writing any data into the cache. Cleaning up dangling
references is a tricky problem, requiring application-level reasoning, and
not even always desirable, since the data could always come back into the
cache later, restoring the validity of the reference.

Previously, I thought it would be helpful to distinguish between the
absence of an entity object and the object simply being empty, but I no
longer think this distinction (which only affected the wording of the
MissingFieldError description) matters very much, and we can simplify
everything by adopting the following policy:

> During cache reads, a dangling Reference should behave as much as
  possible like a Reference to an entity object that happens to contain
  zero fields.

I'm optimistic this policy may help with issues like #6325. At the very
least, this policy means there's now only one flavor of MissingFieldError,
which should reduce confusion.
  • Loading branch information
benjamn committed May 30, 2020
1 parent 13ad552 commit c810713
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/cache/inmemory/__tests__/entityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1555,11 +1555,11 @@ describe('EntityStore', () => {

expect(() => cache.readQuery({
query: queryWithAliases,
})).toThrow(/Dangling reference to missing ABCs:.* object/);
})).toThrow(/Can't find field 'a' on ABCs:.* object/);

expect(() => cache.readQuery({
query: queryWithoutAliases,
})).toThrow(/Dangling reference to missing ABCs:.* object/);
})).toThrow(/Can't find field 'a' on ABCs:.* object/);
});

it("gracefully handles eviction amid optimistic updates", () => {
Expand Down Expand Up @@ -1639,8 +1639,8 @@ describe('EntityStore', () => {

const missing = [
new MissingFieldError(
"Dangling reference to missing Author:2 object",
["book", "author"],
"Can't find field 'name' on Author:2 object",
["book", "author", "name"],
expect.anything(),
expect.anything(),
),
Expand Down
6 changes: 3 additions & 3 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2397,7 +2397,7 @@ describe("type policies", function () {
});

expect(read).toThrow(
/Dangling reference to missing Book:{"isbn":"156858217X"} object/
/Can't find field 'title' on Book:{"isbn":"156858217X"} object/
);

const stealThisData = {
Expand Down Expand Up @@ -2529,11 +2529,11 @@ describe("type policies", function () {
});

expect(() => read("0393354326")).toThrow(
/Dangling reference to missing Book:{"isbn":"0393354326"} object/
/Can't find field 'title' on Book:{"isbn":"0393354326"} object/
);

expect(() => read("156858217X")).toThrow(
/Dangling reference to missing Book:{"isbn":"156858217X"} object/
/Can't find field 'title' on Book:{"isbn":"156858217X"} object/
);
});

Expand Down
14 changes: 0 additions & 14 deletions src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,6 @@ export class StoreReader {
objectOrReference,
context,
}: ExecSelectionSetOptions): ExecResult {
if (isReference(objectOrReference) &&
!context.policies.rootTypenamesById[objectOrReference.__ref] &&
!context.store.has(objectOrReference.__ref)) {
return {
result: {},
missing: [missingFromInvariant(
new InvariantError(
`Dangling reference to missing ${objectOrReference.__ref} object`
),
context,
)],
};
}

const { fragmentMap, variables, policies, store } = context;
const objectsToMerge: { [key: string]: any }[] = [];
const finalResult: ExecResult = { result: null };
Expand Down

0 comments on commit c810713

Please sign in to comment.