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

potential optimization - bail out of executeSubSelectedArray calls #11670

Merged
merged 6 commits into from
Mar 26, 2024
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
5 changes: 5 additions & 0 deletions .changeset/mean-singers-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Bail out of `executeSubSelectedArray` calls if the array has 0 elements.
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39319,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32630
"dist/apollo-client.min.cjs": 39325,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32634
}
63 changes: 63 additions & 0 deletions src/cache/inmemory/__tests__/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,69 @@ describe("reading from the store", () => {
});
});

it("runs a nested query - skips iterating into an empty array", () => {
const reader = new StoreReader({
cache: new InMemoryCache(),
});

const result = {
id: "abcd",
stringField: "This is a string!",
numberField: 5,
nullField: null,
nestedArray: [
{
id: "abcde",
stringField: "This is a string also!",
numberField: 7,
nullField: null,
},
],
emptyArray: [],
} satisfies StoreObject;

const store = defaultNormalizedCacheFactory({
ROOT_QUERY: { ...result, nestedArray: [makeReference("abcde")] },
abcde: result.nestedArray[0],
});

expect(reader["executeSubSelectedArray"].size).toBe(0);

// assumption: cache size does not increase for empty array
readQueryFromStore(reader, {
store,
query: gql`
{
stringField
numberField
emptyArray {
id
stringField
numberField
}
}
`,
});
expect(reader["executeSubSelectedArray"].size).toBe(0);

// assumption: cache size increases for array with content
readQueryFromStore(reader, {
store,
query: gql`
{
stringField
numberField
nestedArray {
id
stringField
numberField
}
}
`,
});
expect(reader["executeSubSelectedArray"].size).toBe(1);
});

it("throws on a missing field", () => {
const result = {
id: "abcd",
Expand Down
20 changes: 11 additions & 9 deletions src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,17 @@ export class StoreReader {
});
}
} else if (isArray(fieldValue)) {
fieldValue = handleMissing(
this.executeSubSelectedArray({
field: selection,
array: fieldValue,
enclosingRef,
context,
}),
resultName
);
if (fieldValue.length > 0) {
fieldValue = handleMissing(
this.executeSubSelectedArray({
field: selection,
array: fieldValue,
enclosingRef,
context,
}),
resultName
);
}
} else if (!selection.selectionSet) {
// If the field does not have a selection set, then we handle it
// as a scalar value. To keep this.canon from canonicalizing
Expand Down