1
1
import type { DocumentNode } from "graphql" ;
2
2
3
+ export type OperationResultType = "stream" | "single" ;
4
+
3
5
export interface TypedDocumentNode <
4
6
Result = { [ key : string ] : any } ,
5
- Variables = { [ key : string ] : any }
7
+ Variables = { [ key : string ] : any } ,
8
+ ResultType extends OperationResultType = any
6
9
> extends DocumentNode {
7
10
/**
8
11
* This type is used to ensure that the variables you pass in to the query are assignable to Variables
9
12
* and that the Result is assignable to whatever you pass your result to. The method is never actually
10
13
* implemented, but the type is valid because we list it as optional
11
14
*/
12
- __apiType ?: ( variables : Variables ) => Result ;
15
+ __apiType ?: ( variables : Variables , resultType : ResultType ) => Result ;
13
16
}
14
17
15
18
/**
@@ -18,10 +21,7 @@ export interface TypedDocumentNode<
18
21
* const myQuery = { ... }; // TypedDocumentNode<R, V>
19
22
* type ResultType = ResultOf<typeof myQuery>; // Now it's R
20
23
*/
21
- export type ResultOf < T > = T extends TypedDocumentNode <
22
- infer ResultType ,
23
- infer VariablesType
24
- >
24
+ export type ResultOf < T > = T extends TypedDocumentNode < infer ResultType , any >
25
25
? ResultType
26
26
: never ;
27
27
@@ -32,8 +32,22 @@ export type ResultOf<T> = T extends TypedDocumentNode<
32
32
* type VariablesType = VariablesOf<typeof myQuery>; // Now it's V
33
33
*/
34
34
export type VariablesOf < T > = T extends TypedDocumentNode <
35
- infer ResultType ,
35
+ any ,
36
36
infer VariablesType
37
37
>
38
38
? VariablesType
39
39
: never ;
40
+
41
+ /**
42
+ * Helper for extracting a TypeScript type for operation type from a TypedDocumentNode.
43
+ * @example
44
+ * const myQuery = { ... }; // TypedDocumentNode<R, V, O>
45
+ * type OperationType = VariablesOf<typeof myQuery>; // Now it's O
46
+ */
47
+ export type OperationTypeOf < T > = T extends TypedDocumentNode <
48
+ any ,
49
+ any ,
50
+ infer OperationType
51
+ >
52
+ ? OperationType
53
+ : never ;
0 commit comments