diff --git a/.changeset/mean-singers-cheer.md b/.changeset/mean-singers-cheer.md new file mode 100644 index 00000000000..b2cb9cd0982 --- /dev/null +++ b/.changeset/mean-singers-cheer.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Bail out of `executeSubSelectedArray` calls if the array has 0 elements. diff --git a/.size-limits.json b/.size-limits.json index 3dc2aae6853..f964b98787f 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -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 } diff --git a/src/cache/inmemory/__tests__/readFromStore.ts b/src/cache/inmemory/__tests__/readFromStore.ts index 2af1138cef8..d936ad73734 100644 --- a/src/cache/inmemory/__tests__/readFromStore.ts +++ b/src/cache/inmemory/__tests__/readFromStore.ts @@ -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", diff --git a/src/cache/inmemory/readFromStore.ts b/src/cache/inmemory/readFromStore.ts index cc1ec9f0f6b..d89876d85c9 100644 --- a/src/cache/inmemory/readFromStore.ts +++ b/src/cache/inmemory/readFromStore.ts @@ -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