Skip to content

Commit 262f207

Browse files
committed
fix: apply query variable defaults when undefined is passed explicitly
When query variables like { show: undefined } are passed, the cache's diffQueryAgainstStore would overwrite GraphQL-specified default values with undefined by spreading the user-provided variables directly. This caused @include/@Skip directives to fail with 'Invalid variable referenced' errors. Filter out undefined values before merging with defaults, matching the behavior already implemented in QueryManager.getVariables. Fixes #13345
1 parent 8518419 commit 262f207

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/cache/inmemory/__tests__/readFromStore.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,39 @@ describe("reading from the store", () => {
262262
});
263263
});
264264

265+
it("applies defaults when variable is explicitly undefined", () => {
266+
const query = gql`
267+
query someQuery($show: Boolean = false) {
268+
id
269+
field @include(if: $show)
270+
}
271+
`;
272+
273+
const variables = {
274+
show: undefined,
275+
};
276+
277+
const store = defaultNormalizedCacheFactory({
278+
ROOT_QUERY: {
279+
__typename: "Query",
280+
id: "abcd",
281+
field: null,
282+
} as StoreObject,
283+
});
284+
285+
const result = readQueryFromStore(reader, {
286+
store,
287+
query,
288+
variables,
289+
});
290+
291+
expect(result).toEqual({
292+
__typename: "Query",
293+
id: "abcd",
294+
field: null,
295+
});
296+
});
297+
265298
it("runs a nested query", () => {
266299
const result: any = {
267300
id: "abcd",

src/cache/inmemory/readFromStore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,12 @@ export class StoreReader {
204204
}: DiffQueryAgainstStoreOptions): Cache.DiffResult<T> {
205205
const policies = this.config.cache.policies;
206206

207+
const rawVariables = variables ?? {};
207208
variables = {
208209
...getDefaultValues(getQueryDefinition(query)),
209-
...variables!,
210+
...Object.fromEntries(
211+
Object.entries(rawVariables).filter(([, v]) => v !== undefined)
212+
),
210213
};
211214

212215
const rootRef = makeReference(rootId);

0 commit comments

Comments
 (0)