From a8bea01b9d9cee70142b2ae7dea48cee5ed2b896 Mon Sep 17 00:00:00 2001 From: Aleksandra Date: Mon, 20 Mar 2023 07:25:29 +0100 Subject: [PATCH] Add `DocumentTypeDecoration` interface with `__apiType` key (#154) --- .changeset/lovely-ghosts-suffer.md | 5 +++++ packages/core/src/index.ts | 21 ++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 .changeset/lovely-ghosts-suffer.md diff --git a/.changeset/lovely-ghosts-suffer.md b/.changeset/lovely-ghosts-suffer.md new file mode 100644 index 0000000..2ed22d4 --- /dev/null +++ b/.changeset/lovely-ghosts-suffer.md @@ -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` diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 7a0c1b5..60e73a4 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,24 +1,27 @@ import type { DocumentNode } from "graphql"; -export interface TypedDocumentNode< - Result = { [key: string]: any }, - Variables = { [key: string]: any } -> extends DocumentNode { +export interface DocumentTypeDecoration { /** * 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 {} + /** - * 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 * type ResultType = ResultOf; // Now it's R */ -export type ResultOf = T extends TypedDocumentNode< +export type ResultOf = T extends DocumentTypeDecoration< infer ResultType, infer VariablesType > @@ -26,12 +29,12 @@ export type ResultOf = T extends TypedDocumentNode< : 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 * type VariablesType = VariablesOf; // Now it's V */ -export type VariablesOf = T extends TypedDocumentNode< +export type VariablesOf = T extends DocumentTypeDecoration< infer ResultType, infer VariablesType >