Skip to content

Commit b6ecff7

Browse files
committed
feat: allow embedding the operation result type
1 parent 3711b12 commit b6ecff7

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/core/src/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { DocumentNode } from 'graphql';
22

3-
export interface TypedDocumentNode<Result = { [key: string]: any }, Variables = { [key: string]: any }> extends DocumentNode {
3+
export type OperationResultType = "stream" | "single";
4+
5+
export interface TypedDocumentNode<Result = { [key: string]: any }, Variables = { [key: string]: any }, ResultType extends OperationResultType = any> extends DocumentNode {
46
/**
57
* This type is used to ensure that the variables you pass in to the query are assignable to Variables
68
* and that the Result is assignable to whatever you pass your result to. The method is never actually
79
* implemented, but the type is valid because we list it as optional
810
*/
9-
__apiType?: (variables: Variables) => Result;
11+
__apiType?: (variables: Variables, resultType: ResultType) => Result;
1012
}
1113

1214
/**
@@ -15,12 +17,20 @@ export interface TypedDocumentNode<Result = { [key: string]: any }, Variables =
1517
* const myQuery = { ... }; // TypedDocumentNode<R, V>
1618
* type ResultType = ResultOf<typeof myQuery>; // Now it's R
1719
*/
18-
export type ResultOf<T> = T extends TypedDocumentNode<infer ResultType, infer VariablesType> ? ResultType : never;
20+
export type ResultOf<T> = T extends TypedDocumentNode<infer ResultType, any> ? ResultType : never;
1921

2022
/**
2123
* Helper for extracting a TypeScript type for operation variables from a TypedDocumentNode.
2224
* @example
2325
* const myQuery = { ... }; // TypedDocumentNode<R, V>
2426
* type VariablesType = VariablesOf<typeof myQuery>; // Now it's V
2527
*/
26-
export type VariablesOf<T> = T extends TypedDocumentNode<infer ResultType, infer VariablesType> ? VariablesType : never;
28+
export type VariablesOf<T> = T extends TypedDocumentNode<any, infer VariablesType> ? VariablesType : never;
29+
30+
/**
31+
* Helper for extracting a TypeScript type for operation type from a TypedDocumentNode.
32+
* @example
33+
* const myQuery = { ... }; // TypedDocumentNode<R, V, O>
34+
* type OperationType = VariablesOf<typeof myQuery>; // Now it's O
35+
*/
36+
export type OperationTypeOf<T> = T extends TypedDocumentNode<any, any, infer OperationType> ? OperationType : never;

0 commit comments

Comments
 (0)