Skip to content

Commit 061959d

Browse files
n1ru4ldotansimha
authored andcommitted
feat: allow embedding the operation result type
1 parent edb47ea commit 061959d

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

packages/core/src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import type { DocumentNode } from "graphql";
22

3+
export type OperationResultType = "stream" | "single";
4+
35
export interface TypedDocumentNode<
46
Result = { [key: string]: any },
5-
Variables = { [key: string]: any }
7+
Variables = { [key: string]: any },
8+
ResultType extends OperationResultType = any
69
> extends DocumentNode {
710
/**
811
* This type is used to ensure that the variables you pass in to the query are assignable to Variables
912
* and that the Result is assignable to whatever you pass your result to. The method is never actually
1013
* implemented, but the type is valid because we list it as optional
1114
*/
12-
__apiType?: (variables: Variables) => Result;
15+
__apiType?: (variables: Variables, resultType: ResultType) => Result;
1316
}
1417

1518
/**
@@ -18,10 +21,7 @@ export interface TypedDocumentNode<
1821
* const myQuery = { ... }; // TypedDocumentNode<R, V>
1922
* type ResultType = ResultOf<typeof myQuery>; // Now it's R
2023
*/
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>
2525
? ResultType
2626
: never;
2727

@@ -32,8 +32,22 @@ export type ResultOf<T> = T extends TypedDocumentNode<
3232
* type VariablesType = VariablesOf<typeof myQuery>; // Now it's V
3333
*/
3434
export type VariablesOf<T> = T extends TypedDocumentNode<
35-
infer ResultType,
35+
any,
3636
infer VariablesType
3737
>
3838
? VariablesType
3939
: 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

Comments
 (0)