Skip to content

Commit d48dab2

Browse files
committed
move AdditionalApolloLinkResultTypes into TypeOverrides
1 parent 7a14c90 commit d48dab2

8 files changed

Lines changed: 70 additions & 27 deletions

File tree

.changeset/four-rockets-live.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@apollo/client": major
3+
---
4+
5+
If you use an incremental delivery handler, you now have to explicitly opt into adding the chunk types to the `ApolloLink.Result` type.
6+
7+
8+
```ts title="apollo-client.d.ts
9+
import { Defer20220824Handler } from "@apollo/client/incremental";
10+
11+
declare module "@apollo/client" {
12+
export interface TypeOverrides extends Defer20220824Handler.TypeOverrides {}
13+
}
14+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { ApolloLink } from "@apollo/client";
2+
import type { FormattedExecutionResult } from "graphql";
3+
4+
// ensure that without manual addition to `TypeOverrides`,
5+
// `ApolloLink.Result` equals `FormattedExecutionResult`
6+
// with no additional alternatives
7+
8+
type TData = { foo: string };
9+
type TExtensions = { bar: number };
10+
expectTypeOf<ApolloLink.Result<TData, TExtensions>>().toEqualTypeOf<
11+
FormattedExecutionResult<TData, TExtensions>
12+
>();

src/incremental/handlers/defer20220824.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
} from "graphql";
66

77
import type { ApolloLink } from "@apollo/client/link";
8-
import type { DeepPartial } from "@apollo/client/utilities";
8+
import type { DeepPartial, HKT } from "@apollo/client/utilities";
99
import {
1010
DeepMerger,
1111
hasDirectives,
@@ -15,6 +15,15 @@ import {
1515
import type { Incremental } from "../types.js";
1616

1717
export declare namespace Defer20220824Handler {
18+
interface Defer20220824Result extends HKT {
19+
arg1: unknown; // TData
20+
arg2: unknown; // TExtensions
21+
return: Defer20220824Handler.Chunk<Record<string, unknown>>;
22+
}
23+
export interface TypeOverrides {
24+
AdditionalApolloLinkResultTypes: Defer20220824Result;
25+
}
26+
1827
export type InitialResult<TData = Record<string, unknown>> = {
1928
data?: TData | null | undefined;
2029
errors?: ReadonlyArray<GraphQLFormattedError>;
@@ -43,12 +52,6 @@ export declare namespace Defer20220824Handler {
4352
};
4453
}
4554

46-
declare module "@apollo/client/link" {
47-
export interface AdditionalApolloLinkResultTypes {
48-
Defer20220824Handler: Defer20220824Handler.Chunk<Record<string, unknown>>;
49-
}
50-
}
51-
5255
class DeferRequest<TData extends Record<string, unknown>>
5356
implements
5457
Incremental.IncrementalRequest<Defer20220824Handler.Chunk<TData>, TData>

src/incremental/handlers/notImplemented.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import type { ApolloLink } from "@apollo/client/link";
2+
import type { HKT } from "@apollo/client/utilities";
23
import { hasDirectives } from "@apollo/client/utilities/internal";
34
import { invariant } from "@apollo/client/utilities/invariant";
45

56
import type { Incremental } from "../types.js";
67

8+
export declare namespace NotImplementedHandler {
9+
interface NotImplementedResult extends HKT {
10+
arg1: unknown; // TData
11+
arg2: unknown; // TExtensions
12+
return: never;
13+
}
14+
export interface TypeOverrides {
15+
AdditionalApolloLinkResultTypes: NotImplementedResult;
16+
}
17+
}
18+
719
export class NotImplementedHandler implements Incremental.Handler<never> {
820
isIncrementalResult(_: any): _ is never {
921
return false;

src/link/core/ApolloLink.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ import type {
1111
DefaultContext,
1212
OperationVariables,
1313
} from "@apollo/client";
14+
import type { TypeOverrides } from "@apollo/client";
15+
import type { NotImplementedHandler } from "@apollo/client/incremental";
1416
import { createOperation } from "@apollo/client/link/utils";
1517
import { __DEV__ } from "@apollo/client/utilities/environment";
18+
import type { ApplyHKTImplementationWithDefault } from "@apollo/client/utilities/internal";
1619
import {
1720
invariant,
1821
newInvariantError,
1922
} from "@apollo/client/utilities/invariant";
2023

21-
import type { AdditionalApolloLinkResultTypes } from "./types.js";
22-
2324
export declare namespace ApolloLink {
2425
/**
2526
* Context provided for link execution, such as the client executing the
@@ -73,15 +74,23 @@ export declare namespace ApolloLink {
7374
forward: ApolloLink.ForwardFunction
7475
) => Observable<ApolloLink.Result>;
7576

77+
export type AdditionalResultTypes<
78+
TData = Record<string, any>,
79+
TExtensions = Record<string, any>,
80+
> = ApplyHKTImplementationWithDefault<
81+
TypeOverrides,
82+
"AdditionalApolloLinkResultTypes",
83+
NotImplementedHandler.TypeOverrides,
84+
TData,
85+
TExtensions
86+
>;
87+
7688
export type Result<
7789
TData = Record<string, any>,
7890
TExtensions = Record<string, any>,
7991
> =
8092
| FormattedExecutionResult<TData, TExtensions>
81-
| AdditionalApolloLinkResultTypes<
82-
TData,
83-
TExtensions
84-
>[keyof AdditionalApolloLinkResultTypes<TData, TExtensions>];
93+
| AdditionalResultTypes<TData, TExtensions>;
8594

8695
/**
8796
* The currently executed operation object provided to an `ApolloLink.RequestHandler`

src/link/core/types.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import type { FormattedExecutionResult, GraphQLFormattedError } from "graphql";
2-
import type { DocumentNode } from "graphql";
3-
4-
export type { DocumentNode };
52

63
export interface ApolloPayloadResult<
74
TData = Record<string, any>,
@@ -12,8 +9,3 @@ export interface ApolloPayloadResult<
129
// these are fatal errors that will include done: true.
1310
errors?: ReadonlyArray<GraphQLFormattedError>;
1411
}
15-
16-
export interface AdditionalApolloLinkResultTypes<
17-
TData = Record<string, any>,
18-
TExtensions = Record<string, any>,
19-
> {}

src/link/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
export type { DocumentNode } from "graphql";
2+
13
export { empty } from "./core/empty.js";
24
export { from } from "./core/from.js";
35
export { split } from "./core/split.js";
46
export { concat } from "./core/concat.js";
57
export { execute } from "./core/execute.js";
68
export { ApolloLink } from "./core/ApolloLink.js";
79

8-
export type {
9-
AdditionalApolloLinkResultTypes,
10-
ApolloPayloadResult,
11-
DocumentNode,
12-
} from "./core/types.js";
10+
export type { ApolloPayloadResult } from "./core/types.js";
1311

1412
export type {
1513
FetchResult,

src/testing/internal/declarations.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Streaming } from "@apollo/client";
2+
import type { Defer20220824Handler } from "@apollo/client/incremental";
23
import type { GraphQLCodegenDataMasking } from "@apollo/client/masking";
34
import type { HKT } from "@apollo/client/utilities";
45
import { DeepPartial } from "@apollo/client/utilities";
@@ -11,9 +12,11 @@ type StreamingOverride<TData> = TData & { __streaming?: true };
1112
interface StreamingOverrideHKT extends HKT {
1213
return: StreamingOverride<this["arg1"]>;
1314
}
15+
1416
declare module "@apollo/client" {
1517
export interface TypeOverrides
16-
extends GraphQLCodegenDataMasking.TypeOverrides {
18+
extends GraphQLCodegenDataMasking.TypeOverrides,
19+
Defer20220824Handler.TypeOverrides {
1720
Streaming: StreamingOverrideHKT;
1821
}
1922
}

0 commit comments

Comments
 (0)