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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
- In addition to the `result.data` property, `useQuery` and `useLazyQuery` will now provide a `result.previousData` property, which can be useful when a network request is pending and `result.data` is undefined, since `result.previousData` can be rendered instead of rendering an empty/loading state. <br/>
[@hwillson](https://github.com/hwillson) in [#7082](https://github.com/apollographql/apollo-client/pull/7082)

- Provide default empty cache object for root IDs like `ROOT_QUERY`, to avoid differences in behavior before/after `ROOT_QUERY` data has been written into `InMemoryCache`. <br/>
[@benjamn](https://github.com/benjamn) in [#7100](https://github.com/apollographql/apollo-client/pull/7100)

## Apollo Client 3.2.1

## Bug Fixes
Expand Down
52 changes: 52 additions & 0 deletions src/cache/inmemory/__tests__/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,58 @@ describe('reading from the store', () => {
});
});

it("read functions for root query fields work with empty cache", () => {
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
uuid() {
return "8d573b9c-cfcf-4e3e-98dd-14d255af577e";
},
null() {
return null;
},
}
},
},
});

expect(cache.readQuery({
query: gql`query { uuid null }`,
})).toEqual({
uuid: "8d573b9c-cfcf-4e3e-98dd-14d255af577e",
null: null,
});

expect(cache.extract()).toEqual({});

expect(cache.readFragment({
id: "ROOT_QUERY",
fragment: gql`
fragment UUIDFragment on Query {
null
uuid
}
`,
})).toEqual({
uuid: "8d573b9c-cfcf-4e3e-98dd-14d255af577e",
null: null,
});

expect(cache.extract()).toEqual({});

expect(cache.readFragment({
id: "does not exist",
fragment: gql`
fragment F on Never {
whatever
}
`,
})).toBe(null);

expect(cache.extract()).toEqual({});
});

it("custom read functions can map/filter dangling references", () => {
const cache = new InMemoryCache({
typePolicies: {
Expand Down
14 changes: 12 additions & 2 deletions src/cache/inmemory/entityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,18 @@ export abstract class EntityStore implements NormalizedCache {
// should not rely on this dependency, since the contents could change
// without the object being added or removed.
if (dependOnExistence) this.group.depend(dataId, "__exists");
return hasOwn.call(this.data, dataId) ? this.data[dataId] :
this instanceof Layer ? this.parent.lookup(dataId, dependOnExistence) : void 0;

if (hasOwn.call(this.data, dataId)) {
return this.data[dataId];
}

if (this instanceof Layer) {
return this.parent.lookup(dataId, dependOnExistence);
}

if (this.policies.rootTypenamesById[dataId]) {
return Object.create(null);
}
}

public merge(dataId: string, incoming: StoreObject): void {
Expand Down