From 60592e95399c3695d1d49a4c39ad29f00d4059fd Mon Sep 17 00:00:00 2001 From: "Henry Q. Dineen" Date: Wed, 24 Apr 2024 14:20:57 -0400 Subject: [PATCH] Fix operation.setContext() type (#11756) * Fix operation.setContext() type * result should be `Partial` Co-authored-by: Lenz Weber-Tronic * use getContext/setContext types in createOperation impl --------- Co-authored-by: Lenz Weber-Tronic Co-authored-by: Lenz Weber-Tronic --- .api-reports/api-report-core.md | 5 ++++- .api-reports/api-report-link_batch-http.md | 5 ++++- .api-reports/api-report-link_batch.md | 5 ++++- .api-reports/api-report-link_context.md | 5 ++++- .api-reports/api-report-link_core.md | 5 ++++- .api-reports/api-report-link_error.md | 5 ++++- .api-reports/api-report-link_http.md | 5 ++++- .api-reports/api-report-link_persisted-queries.md | 5 ++++- .api-reports/api-report-link_remove-typename.md | 5 ++++- .api-reports/api-report-link_retry.md | 5 ++++- .api-reports/api-report-link_schema.md | 5 ++++- .api-reports/api-report-link_subscriptions.md | 5 ++++- .api-reports/api-report-link_utils.md | 5 ++++- .api-reports/api-report-link_ws.md | 5 ++++- .api-reports/api-report-react.md | 5 ++++- .api-reports/api-report-react_components.md | 5 ++++- .api-reports/api-report-react_context.md | 5 ++++- .api-reports/api-report-react_hoc.md | 5 ++++- .api-reports/api-report-react_hooks.md | 5 ++++- .api-reports/api-report-react_internal.md | 5 ++++- .api-reports/api-report-react_ssr.md | 5 ++++- .api-reports/api-report-testing.md | 5 ++++- .api-reports/api-report-testing_core.md | 5 ++++- .api-reports/api-report-utilities.md | 5 ++++- .api-reports/api-report.md | 5 ++++- .changeset/strange-needles-suffer.md | 5 +++++ src/link/core/types.ts | 9 ++++++++- src/link/utils/createOperation.ts | 4 ++-- 28 files changed, 115 insertions(+), 28 deletions(-) create mode 100644 .changeset/strange-needles-suffer.md diff --git a/.api-reports/api-report-core.md b/.api-reports/api-report-core.md index aabdc975485..88f23bc6491 100644 --- a/.api-reports/api-report-core.md +++ b/.api-reports/api-report-core.md @@ -1598,7 +1598,10 @@ export interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_batch-http.md b/.api-reports/api-report-link_batch-http.md index ce5dded3739..89008cc6985 100644 --- a/.api-reports/api-report-link_batch-http.md +++ b/.api-reports/api-report-link_batch-http.md @@ -204,7 +204,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_batch.md b/.api-reports/api-report-link_batch.md index a547973287d..6f6464edbbd 100644 --- a/.api-reports/api-report-link_batch.md +++ b/.api-reports/api-report-link_batch.md @@ -178,7 +178,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_context.md b/.api-reports/api-report-link_context.md index af79db73e52..76a7c4e5344 100644 --- a/.api-reports/api-report-link_context.md +++ b/.api-reports/api-report-link_context.md @@ -149,7 +149,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_core.md b/.api-reports/api-report-link_core.md index f488d284b51..ce472253f3c 100644 --- a/.api-reports/api-report-link_core.md +++ b/.api-reports/api-report-link_core.md @@ -149,7 +149,10 @@ export interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_error.md b/.api-reports/api-report-link_error.md index af048d6fe6b..245cc7946c9 100644 --- a/.api-reports/api-report-link_error.md +++ b/.api-reports/api-report-link_error.md @@ -198,7 +198,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_http.md b/.api-reports/api-report-link_http.md index fbd71df7348..f51aeded9bb 100644 --- a/.api-reports/api-report-link_http.md +++ b/.api-reports/api-report-link_http.md @@ -242,7 +242,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_persisted-queries.md b/.api-reports/api-report-link_persisted-queries.md index 14e7a0b47db..7a977f4ce55 100644 --- a/.api-reports/api-report-link_persisted-queries.md +++ b/.api-reports/api-report-link_persisted-queries.md @@ -204,7 +204,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_remove-typename.md b/.api-reports/api-report-link_remove-typename.md index f50798f5f02..05dcca3dac0 100644 --- a/.api-reports/api-report-link_remove-typename.md +++ b/.api-reports/api-report-link_remove-typename.md @@ -155,7 +155,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_retry.md b/.api-reports/api-report-link_retry.md index a4a61a6ea1d..173a281dd67 100644 --- a/.api-reports/api-report-link_retry.md +++ b/.api-reports/api-report-link_retry.md @@ -159,7 +159,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_schema.md b/.api-reports/api-report-link_schema.md index fcbee50828b..14459f745e9 100644 --- a/.api-reports/api-report-link_schema.md +++ b/.api-reports/api-report-link_schema.md @@ -147,7 +147,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_subscriptions.md b/.api-reports/api-report-link_subscriptions.md index 8745a5772cb..a67c5415721 100644 --- a/.api-reports/api-report-link_subscriptions.md +++ b/.api-reports/api-report-link_subscriptions.md @@ -158,7 +158,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_utils.md b/.api-reports/api-report-link_utils.md index 061cad690e5..edce7cda76d 100644 --- a/.api-reports/api-report-link_utils.md +++ b/.api-reports/api-report-link_utils.md @@ -55,7 +55,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-link_ws.md b/.api-reports/api-report-link_ws.md index 72a8165e4f0..7969b0cdbc1 100644 --- a/.api-reports/api-report-link_ws.md +++ b/.api-reports/api-report-link_ws.md @@ -148,7 +148,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-react.md b/.api-reports/api-report-react.md index c72fe91334c..9058ec39286 100644 --- a/.api-reports/api-report-react.md +++ b/.api-reports/api-report-react.md @@ -1421,7 +1421,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: Context) => Context; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: Context) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-react_components.md b/.api-reports/api-report-react_components.md index ff3554ad73e..251839f2f28 100644 --- a/.api-reports/api-report-react_components.md +++ b/.api-reports/api-report-react_components.md @@ -1219,7 +1219,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-react_context.md b/.api-reports/api-report-react_context.md index 57b75b6f9eb..18cf1de1330 100644 --- a/.api-reports/api-report-react_context.md +++ b/.api-reports/api-report-react_context.md @@ -1158,7 +1158,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-react_hoc.md b/.api-reports/api-report-react_hoc.md index 671fa20ca12..9e7746e9d73 100644 --- a/.api-reports/api-report-react_hoc.md +++ b/.api-reports/api-report-react_hoc.md @@ -1179,7 +1179,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-react_hooks.md b/.api-reports/api-report-react_hooks.md index ddd545d3c7e..e5ba909b727 100644 --- a/.api-reports/api-report-react_hooks.md +++ b/.api-reports/api-report-react_hooks.md @@ -1368,7 +1368,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-react_internal.md b/.api-reports/api-report-react_internal.md index 0be4d2b9cb8..69aa5c05e09 100644 --- a/.api-reports/api-report-react_internal.md +++ b/.api-reports/api-report-react_internal.md @@ -1254,7 +1254,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-react_ssr.md b/.api-reports/api-report-react_ssr.md index 7b183853465..e064686bc36 100644 --- a/.api-reports/api-report-react_ssr.md +++ b/.api-reports/api-report-react_ssr.md @@ -1143,7 +1143,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-testing.md b/.api-reports/api-report-testing.md index 2c1e6dd0195..badae387286 100644 --- a/.api-reports/api-report-testing.md +++ b/.api-reports/api-report-testing.md @@ -1251,7 +1251,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-testing_core.md b/.api-reports/api-report-testing_core.md index da8706e0df2..48098dce8b5 100644 --- a/.api-reports/api-report-testing_core.md +++ b/.api-reports/api-report-testing_core.md @@ -1206,7 +1206,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report-utilities.md b/.api-reports/api-report-utilities.md index 9d573f7a59c..49b55f0b20d 100644 --- a/.api-reports/api-report-utilities.md +++ b/.api-reports/api-report-utilities.md @@ -1907,7 +1907,10 @@ interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.api-reports/api-report.md b/.api-reports/api-report.md index e9290fed8eb..9b72b6ae88b 100644 --- a/.api-reports/api-report.md +++ b/.api-reports/api-report.md @@ -1924,7 +1924,10 @@ export interface Operation { // (undocumented) query: DocumentNode; // (undocumented) - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + (updateContext: (previousContext: DefaultContext) => Partial): void; + }; // (undocumented) variables: Record; } diff --git a/.changeset/strange-needles-suffer.md b/.changeset/strange-needles-suffer.md new file mode 100644 index 00000000000..23cd112467b --- /dev/null +++ b/.changeset/strange-needles-suffer.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Fix operation.setContext() type diff --git a/src/link/core/types.ts b/src/link/core/types.ts index 37fdae3cb57..a898f1a598e 100644 --- a/src/link/core/types.ts +++ b/src/link/core/types.ts @@ -76,7 +76,14 @@ export interface Operation { variables: Record; operationName: string; extensions: Record; - setContext: (context: DefaultContext) => DefaultContext; + setContext: { + (context: Partial): void; + ( + updateContext: ( + previousContext: DefaultContext + ) => Partial + ): void; + }; getContext: () => DefaultContext; } diff --git a/src/link/utils/createOperation.ts b/src/link/utils/createOperation.ts index db9ad4b48a8..5093711a54f 100644 --- a/src/link/utils/createOperation.ts +++ b/src/link/utils/createOperation.ts @@ -5,14 +5,14 @@ export function createOperation( operation: GraphQLRequest ): Operation { let context = { ...starting }; - const setContext = (next: any) => { + const setContext: Operation["setContext"] = (next) => { if (typeof next === "function") { context = { ...context, ...next(context) }; } else { context = { ...context, ...next }; } }; - const getContext = () => ({ ...context }); + const getContext: Operation["getContext"] = () => ({ ...context }); Object.defineProperty(operation, "setContext", { enumerable: false,