Skip to content

Commit

Permalink
feat: allow embedding the operation result type
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Feb 3, 2023
1 parent 3711b12 commit b6ecff7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { DocumentNode } from 'graphql';

export interface TypedDocumentNode<Result = { [key: string]: any }, Variables = { [key: string]: any }> extends DocumentNode {
export type OperationResultType = "stream" | "single";

export interface TypedDocumentNode<Result = { [key: string]: any }, Variables = { [key: string]: any }, ResultType extends OperationResultType = any> extends DocumentNode {
/**
* This type is used to ensure that the variables you pass in to the query are assignable to Variables
* and that the Result is assignable to whatever you pass your result to. The method is never actually
* implemented, but the type is valid because we list it as optional
*/
__apiType?: (variables: Variables) => Result;
__apiType?: (variables: Variables, resultType: ResultType) => Result;
}

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

/**
* Helper for extracting a TypeScript type for operation variables from a TypedDocumentNode.
* @example
* const myQuery = { ... }; // TypedDocumentNode<R, V>
* type VariablesType = VariablesOf<typeof myQuery>; // Now it's V
*/
export type VariablesOf<T> = T extends TypedDocumentNode<infer ResultType, infer VariablesType> ? VariablesType : never;
export type VariablesOf<T> = T extends TypedDocumentNode<any, infer VariablesType> ? VariablesType : never;

/**
* Helper for extracting a TypeScript type for operation type from a TypedDocumentNode.
* @example
* const myQuery = { ... }; // TypedDocumentNode<R, V, O>
* type OperationType = VariablesOf<typeof myQuery>; // Now it's O
*/
export type OperationTypeOf<T> = T extends TypedDocumentNode<any, any, infer OperationType> ? OperationType : never;

0 comments on commit b6ecff7

Please sign in to comment.