Skip to content

Commit

Permalink
ensure defaultContext is also used for mutations and subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 27, 2023
1 parent 0b3c308 commit a6d66e9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-hats-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

ensure `defaultContext` is also used for mutations and subscriptions
6 changes: 2 additions & 4 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1161,9 +1160,7 @@ export class QueryManager<TStore> {
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
),

Expand Down Expand Up @@ -1676,6 +1673,7 @@ export class QueryManager<TStore> {
private prepareContext(context = {}) {
const newContext = this.localState.prepareContext(context);
return {
...this.defaultContext,
...newContext,
clientAwareness: this.clientAwareness,
};
Expand Down
11 changes: 8 additions & 3 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
}
Expand Down

0 comments on commit a6d66e9

Please sign in to comment.