Skip to content

Commit

Permalink
Add DocumentTypeDecoration interface with __apiType key (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
beerose authored Mar 20, 2023
1 parent c4b7328 commit a8bea01
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-ghosts-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-typed-document-node/core": minor
---

Export `DocumentTypeDecoration` interface with `__apiType` key so that it can be used to extend types other than `DocumentNode`
21 changes: 12 additions & 9 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import type { DocumentNode } from "graphql";

export interface TypedDocumentNode<
Result = { [key: string]: any },
Variables = { [key: string]: any }
> extends DocumentNode {
export interface DocumentTypeDecoration<TResult, TVariables> {
/**
* 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: TVariables) => TResult;
}

export interface TypedDocumentNode<
TResult = { [key: string]: any },
TVariables = { [key: string]: any }
> extends DocumentNode,
DocumentTypeDecoration<TResult, TVariables> {}

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

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

0 comments on commit a8bea01

Please sign in to comment.