diff --git a/.api-reports/api-report-incremental.api.md b/.api-reports/api-report-incremental.api.md index 0a2add87232..efdd684674c 100644 --- a/.api-reports/api-report-incremental.api.md +++ b/.api-reports/api-report-incremental.api.md @@ -9,12 +9,22 @@ import type { DeepPartial } from '@apollo/client/utilities'; import type { DocumentNode } from 'graphql'; import type { FormattedExecutionResult } from 'graphql'; import type { GraphQLFormattedError } from 'graphql'; +import type { HKT } from '@apollo/client/utilities'; // @public (undocumented) namespace Defer20220824Handler { // (undocumented) type Chunk> = InitialResult | SubsequentResult; // (undocumented) + interface Defer20220824Result extends HKT { + // (undocumented) + arg1: unknown; + // (undocumented) + arg2: unknown; + // (undocumented) + return: Defer20220824Handler.Chunk>; + } + // (undocumented) type IncrementalDeferPayload> = { data?: TData | null | undefined; errors?: ReadonlyArray; @@ -37,6 +47,11 @@ namespace Defer20220824Handler { hasNext: boolean; incremental?: Array>; }; + // (undocumented) + interface TypeOverrides { + // (undocumented) + AdditionalApolloLinkResultTypes: Defer20220824Result; + } } // @public @@ -91,6 +106,24 @@ export namespace Incremental { export type Path = ReadonlyArray; } +// @public (undocumented) +export namespace NotImplementedHandler { + // (undocumented) + export interface NotImplementedResult extends HKT { + // (undocumented) + arg1: unknown; + // (undocumented) + arg2: unknown; + // (undocumented) + return: never; + } + // (undocumented) + export interface TypeOverrides { + // (undocumented) + AdditionalApolloLinkResultTypes: NotImplementedResult; + } +} + // @public (undocumented) export class NotImplementedHandler implements Incremental.Handler { // (undocumented) diff --git a/.api-reports/api-report-link.api.md b/.api-reports/api-report-link.api.md index ccaa0c8c6cc..8eb1f611efe 100644 --- a/.api-reports/api-report-link.api.md +++ b/.api-reports/api-report-link.api.md @@ -5,20 +5,21 @@ ```ts import type { ApolloClient } from '@apollo/client'; +import type { ApplyHKTImplementationWithDefault } from '@apollo/client/utilities/internal'; import type { DefaultContext } from '@apollo/client'; -import type { DocumentNode } from 'graphql'; +import { DocumentNode } from 'graphql'; import type { FormattedExecutionResult } from 'graphql'; import type { GraphQLFormattedError } from 'graphql'; +import type { NotImplementedHandler } from '@apollo/client/incremental'; import type { Observable } from 'rxjs'; import type { OperationTypeNode } from 'graphql'; import type { OperationVariables } from '@apollo/client'; - -// @public (undocumented) -export interface AdditionalApolloLinkResultTypes, TExtensions = Record> { -} +import type { TypeOverrides } from '@apollo/client'; // @public (undocumented) export namespace ApolloLink { + // (undocumented) + export type AdditionalResultTypes, TExtensions = Record> = ApplyHKTImplementationWithDefault; // (undocumented) export namespace DocumentationTypes { export function ForwardFunction(operation: ApolloLink.Operation): Observable; @@ -51,7 +52,7 @@ export namespace ApolloLink { } export type RequestHandler = (operation: ApolloLink.Operation, forward: ApolloLink.ForwardFunction) => Observable; // (undocumented) - export type Result, TExtensions = Record> = FormattedExecutionResult | AdditionalApolloLinkResultTypes[keyof AdditionalApolloLinkResultTypes]; + export type Result, TExtensions = Record> = FormattedExecutionResult | AdditionalResultTypes; } // @public diff --git a/.api-reports/api-report-testing.api.md b/.api-reports/api-report-testing.api.md index 9dc6bb3249e..8af7cd7e04c 100644 --- a/.api-reports/api-report-testing.api.md +++ b/.api-reports/api-report-testing.api.md @@ -6,6 +6,7 @@ import { ApolloLink } from '@apollo/client/link'; import type { DocumentNode } from 'graphql'; +import { FormattedExecutionResult } from 'graphql'; import { Observable } from 'rxjs'; import type { OperationVariables } from '@apollo/client'; import type { Unmasked } from '@apollo/client/masking'; @@ -108,7 +109,7 @@ export class MockSubscriptionLink extends ApolloLink { // (undocumented) operation?: ApolloLink.Operation; // (undocumented) - request(operation: ApolloLink.Operation): Observable, Record>>; + request(operation: ApolloLink.Operation): Observable, Record>>; // (undocumented) setups: any[]; // (undocumented) diff --git a/.api-reports/api-report.api.md b/.api-reports/api-report.api.md index 368d90feac6..ae6b2825da9 100644 --- a/.api-reports/api-report.api.md +++ b/.api-reports/api-report.api.md @@ -7,7 +7,7 @@ import type { ASTNode } from 'graphql'; import { disableExperimentalFragmentVariables } from 'graphql-tag'; import { disableFragmentWarnings } from 'graphql-tag'; -import type { DocumentNode } from 'graphql'; +import { DocumentNode } from 'graphql'; import { enableExperimentalFragmentVariables } from 'graphql-tag'; import type { FieldNode } from 'graphql'; import type { FormattedExecutionResult } from 'graphql'; @@ -29,10 +29,6 @@ import type { Subscription } from 'rxjs'; import { Trie } from '@wry/trie'; import { TypedDocumentNode } from '@graphql-typed-document-node/core'; -// @public (undocumented) -interface AdditionalApolloLinkResultTypes, TExtensions = Record> { -} - // Warning: (ae-forgotten-export) The symbol "Modifier" needs to be exported by the entry point index.d.ts // Warning: (ae-forgotten-export) The symbol "StoreObjectValueMaybeReference" needs to be exported by the entry point index.d.ts // @@ -389,6 +385,11 @@ export type ApolloClientOptions = ApolloClient.Options; // @public (undocumented) export namespace ApolloLink { + // Warning: (ae-forgotten-export) The symbol "ApplyHKTImplementationWithDefault" needs to be exported by the entry point index.d.ts + // Warning: (ae-forgotten-export) The symbol "NotImplementedHandler" needs to be exported by the entry point index.d.ts + // + // (undocumented) + export type AdditionalResultTypes, TExtensions = Record> = ApplyHKTImplementationWithDefault; // (undocumented) export namespace DocumentationTypes { export function ForwardFunction(operation: ApolloLink.Operation): Observable; @@ -420,10 +421,8 @@ export namespace ApolloLink { variables?: OperationVariables; } export type RequestHandler = (operation: ApolloLink.Operation, forward: ApolloLink.ForwardFunction) => Observable; - // Warning: (ae-forgotten-export) The symbol "AdditionalApolloLinkResultTypes" needs to be exported by the entry point index.d.ts - // // (undocumented) - export type Result, TExtensions = Record> = FormattedExecutionResult | AdditionalApolloLinkResultTypes[keyof AdditionalApolloLinkResultTypes]; + export type Result, TExtensions = Record> = FormattedExecutionResult | AdditionalResultTypes; } // @public @@ -829,7 +828,6 @@ export type DataState = { // @public (undocumented) export namespace DataValue { - // Warning: (ae-forgotten-export) The symbol "ApplyHKTImplementationWithDefault" needs to be exported by the entry point index.d.ts // Warning: (ae-forgotten-export) The symbol "OverridableTypes" needs to be exported by the entry point index.d.ts export type Complete = ApplyHKTImplementationWithDefault; export type Partial = ApplyHKTImplementationWithDefault; @@ -1832,6 +1830,38 @@ export interface NormalizedCacheObject { // @public export type NormalizedExecutionResult, TExtensions = Record> = Omit, "data"> & GetDataState; +// @public (undocumented) +namespace NotImplementedHandler { + // (undocumented) + interface NotImplementedResult extends HKT { + // (undocumented) + arg1: unknown; + // (undocumented) + arg2: unknown; + // (undocumented) + return: never; + } + // (undocumented) + interface TypeOverrides { + // Warning: (ae-forgotten-export) The symbol "NotImplementedHandler" needs to be exported by the entry point index.d.ts + // + // (undocumented) + AdditionalApolloLinkResultTypes: NotImplementedResult; + } +} + +// @public (undocumented) +class NotImplementedHandler implements Incremental.Handler { + // (undocumented) + extractErrors(): void; + // (undocumented) + isIncrementalResult(_: any): _ is never; + // (undocumented) + prepareRequest(request: ApolloLink.Request): ApolloLink.Request; + // (undocumented) + startRequest: any; +} + export { Observable } // @public (undocumented) diff --git a/.changeset/four-rockets-live.md b/.changeset/four-rockets-live.md new file mode 100644 index 00000000000..a98757de067 --- /dev/null +++ b/.changeset/four-rockets-live.md @@ -0,0 +1,14 @@ +--- +"@apollo/client": major +--- + +If you use an incremental delivery handler, you now have to explicitly opt into adding the chunk types to the `ApolloLink.Result` type. + + +```ts title="apollo-client.d.ts +import { Defer20220824Handler } from "@apollo/client/incremental"; + +declare module "@apollo/client" { + export interface TypeOverrides extends Defer20220824Handler.TypeOverrides {} +} +``` diff --git a/.size-limits.json b/.size-limits.json index 97d9abc113e..6b8af1f4ab0 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,6 +1,6 @@ { - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43855, - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38649, - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33615, - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27649 + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43879, + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38651, + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33590, + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27664 } diff --git a/integration-tests/vite/src/ApolloLinkResultType.ts b/integration-tests/vite/src/ApolloLinkResultType.ts new file mode 100644 index 00000000000..c7d44ff4076 --- /dev/null +++ b/integration-tests/vite/src/ApolloLinkResultType.ts @@ -0,0 +1,13 @@ +import type { ApolloLink } from "@apollo/client"; +import { expectTypeOf } from "expect-type"; +import type { FormattedExecutionResult } from "graphql"; + +// ensure that without manual addition to `TypeOverrides`, +// `ApolloLink.Result` equals `FormattedExecutionResult` +// with no additional alternatives + +type TData = { foo: string }; +type TExtensions = { bar: number }; +expectTypeOf>().toEqualTypeOf< + FormattedExecutionResult +>(); diff --git a/src/incremental/handlers/defer20220824.ts b/src/incremental/handlers/defer20220824.ts index a63f6a8c64b..0ed7cd97f59 100644 --- a/src/incremental/handlers/defer20220824.ts +++ b/src/incremental/handlers/defer20220824.ts @@ -5,7 +5,7 @@ import type { } from "graphql"; import type { ApolloLink } from "@apollo/client/link"; -import type { DeepPartial } from "@apollo/client/utilities"; +import type { DeepPartial, HKT } from "@apollo/client/utilities"; import { DeepMerger, hasDirectives, @@ -15,6 +15,15 @@ import { import type { Incremental } from "../types.js"; export declare namespace Defer20220824Handler { + interface Defer20220824Result extends HKT { + arg1: unknown; // TData + arg2: unknown; // TExtensions + return: Defer20220824Handler.Chunk>; + } + export interface TypeOverrides { + AdditionalApolloLinkResultTypes: Defer20220824Result; + } + export type InitialResult> = { data?: TData | null | undefined; errors?: ReadonlyArray; @@ -43,12 +52,6 @@ export declare namespace Defer20220824Handler { }; } -declare module "@apollo/client/link" { - export interface AdditionalApolloLinkResultTypes { - Defer20220824Handler: Defer20220824Handler.Chunk>; - } -} - class DeferRequest> implements Incremental.IncrementalRequest, TData> diff --git a/src/incremental/handlers/notImplemented.ts b/src/incremental/handlers/notImplemented.ts index b7db2eb29d2..f4c02545c57 100644 --- a/src/incremental/handlers/notImplemented.ts +++ b/src/incremental/handlers/notImplemented.ts @@ -1,9 +1,21 @@ import type { ApolloLink } from "@apollo/client/link"; +import type { HKT } from "@apollo/client/utilities"; import { hasDirectives } from "@apollo/client/utilities/internal"; import { invariant } from "@apollo/client/utilities/invariant"; import type { Incremental } from "../types.js"; +export declare namespace NotImplementedHandler { + interface NotImplementedResult extends HKT { + arg1: unknown; // TData + arg2: unknown; // TExtensions + return: never; + } + export interface TypeOverrides { + AdditionalApolloLinkResultTypes: NotImplementedResult; + } +} + export class NotImplementedHandler implements Incremental.Handler { isIncrementalResult(_: any): _ is never { return false; diff --git a/src/link/core/ApolloLink.ts b/src/link/core/ApolloLink.ts index 6934621adb7..9117511ca3d 100644 --- a/src/link/core/ApolloLink.ts +++ b/src/link/core/ApolloLink.ts @@ -11,15 +11,16 @@ import type { DefaultContext, OperationVariables, } from "@apollo/client"; +import type { TypeOverrides } from "@apollo/client"; +import type { NotImplementedHandler } from "@apollo/client/incremental"; import { createOperation } from "@apollo/client/link/utils"; import { __DEV__ } from "@apollo/client/utilities/environment"; +import type { ApplyHKTImplementationWithDefault } from "@apollo/client/utilities/internal"; import { invariant, newInvariantError, } from "@apollo/client/utilities/invariant"; -import type { AdditionalApolloLinkResultTypes } from "./types.js"; - export declare namespace ApolloLink { /** * Context provided for link execution, such as the client executing the @@ -73,15 +74,23 @@ export declare namespace ApolloLink { forward: ApolloLink.ForwardFunction ) => Observable; + export type AdditionalResultTypes< + TData = Record, + TExtensions = Record, + > = ApplyHKTImplementationWithDefault< + TypeOverrides, + "AdditionalApolloLinkResultTypes", + NotImplementedHandler.TypeOverrides, + TData, + TExtensions + >; + export type Result< TData = Record, TExtensions = Record, > = | FormattedExecutionResult - | AdditionalApolloLinkResultTypes< - TData, - TExtensions - >[keyof AdditionalApolloLinkResultTypes]; + | AdditionalResultTypes; /** * The currently executed operation object provided to an `ApolloLink.RequestHandler` diff --git a/src/link/core/types.ts b/src/link/core/types.ts index 3380bbdc9fb..9370cac8ba2 100644 --- a/src/link/core/types.ts +++ b/src/link/core/types.ts @@ -1,7 +1,4 @@ import type { FormattedExecutionResult, GraphQLFormattedError } from "graphql"; -import type { DocumentNode } from "graphql"; - -export type { DocumentNode }; export interface ApolloPayloadResult< TData = Record, @@ -12,8 +9,3 @@ export interface ApolloPayloadResult< // these are fatal errors that will include done: true. errors?: ReadonlyArray; } - -export interface AdditionalApolloLinkResultTypes< - TData = Record, - TExtensions = Record, -> {} diff --git a/src/link/index.ts b/src/link/index.ts index 959146621b9..2a92a91d821 100644 --- a/src/link/index.ts +++ b/src/link/index.ts @@ -1,3 +1,5 @@ +export type { DocumentNode } from "graphql"; + export { empty } from "./core/empty.js"; export { from } from "./core/from.js"; export { split } from "./core/split.js"; @@ -5,11 +7,7 @@ export { concat } from "./core/concat.js"; export { execute } from "./core/execute.js"; export { ApolloLink } from "./core/ApolloLink.js"; -export type { - AdditionalApolloLinkResultTypes, - ApolloPayloadResult, - DocumentNode, -} from "./core/types.js"; +export type { ApolloPayloadResult } from "./core/types.js"; export type { FetchResult, diff --git a/src/testing/internal/declarations.d.ts b/src/testing/internal/declarations.d.ts index 5945cef8f2a..3572714f5d6 100644 --- a/src/testing/internal/declarations.d.ts +++ b/src/testing/internal/declarations.d.ts @@ -1,4 +1,5 @@ import { Streaming } from "@apollo/client"; +import type { Defer20220824Handler } from "@apollo/client/incremental"; import type { GraphQLCodegenDataMasking } from "@apollo/client/masking"; import type { HKT } from "@apollo/client/utilities"; import { DeepPartial } from "@apollo/client/utilities"; @@ -11,9 +12,11 @@ type StreamingOverride = TData & { __streaming?: true }; interface StreamingOverrideHKT extends HKT { return: StreamingOverride; } + declare module "@apollo/client" { export interface TypeOverrides - extends GraphQLCodegenDataMasking.TypeOverrides { + extends GraphQLCodegenDataMasking.TypeOverrides, + Defer20220824Handler.TypeOverrides { Streaming: StreamingOverrideHKT; } }