From a6d66e941d563192dbbc1b319dc2059ff9978fe2 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Mon, 27 Nov 2023 12:18:00 +0100 Subject: [PATCH] ensure `defaultContext` is also used for mutations and subscriptions --- .changeset/quick-hats-marry.md | 5 +++++ src/core/QueryManager.ts | 6 ++---- src/core/__tests__/QueryManager/index.ts | 11 ++++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 .changeset/quick-hats-marry.md diff --git a/.changeset/quick-hats-marry.md b/.changeset/quick-hats-marry.md new file mode 100644 index 00000000000..2667f0a9750 --- /dev/null +++ b/.changeset/quick-hats-marry.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +ensure `defaultContext` is also used for mutations and subscriptions diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index e19cfdd71a3..98ab4d1d1f4 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -8,7 +8,6 @@ import { equal } from "@wry/equality"; import type { ApolloLink, FetchResult } from "../link/core/index.js"; import { execute } from "../link/core/index.js"; import { - compact, hasDirectives, isExecutionPatchIncrementalResult, isExecutionPatchResult, @@ -1161,9 +1160,7 @@ export class QueryManager { return asyncMap( this.getObservableFromLink( linkDocument, - // explicitly a shallow merge so any class instances etc. a user might - // put in here will not be merged into each other. - compact(this.defaultContext, options.context), + options.context, options.variables ), @@ -1676,6 +1673,7 @@ export class QueryManager { private prepareContext(context = {}) { const newContext = this.localState.prepareContext(context); return { + ...this.defaultContext, ...newContext, clientAwareness: this.clientAwareness, }; diff --git a/src/core/__tests__/QueryManager/index.ts b/src/core/__tests__/QueryManager/index.ts index be28a6b575e..4435bf5515d 100644 --- a/src/core/__tests__/QueryManager/index.ts +++ b/src/core/__tests__/QueryManager/index.ts @@ -6180,7 +6180,11 @@ describe("QueryManager", () => { }).toThrowError(/Cannot set property defaultContext/); }); - it("`defaultContext` will be applied to the context of a query", async () => { + it.each([ + ["query", {method: "query", option: "query"}], + ["mutation", {method:"mutate", option: "mutation"}], + ["subscription", {method:"subscribe", option: "query"}] + ]as const)("`defaultContext` will be applied to the context of a %s", async (_, {method, option}) => { let context: any; const client = new ApolloClient({ cache: new InMemoryCache(), @@ -6196,8 +6200,9 @@ describe("QueryManager", () => { }, }); - await client.query({ - query: gql` + // @ts-ignore a bit too generic for TS + client[method]({ + [option]: gql` query { foo }