From 208c0a10a18f6d6cae6aba7cbd4e2df1fdffabb7 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Thu, 22 Aug 2024 12:29:18 +0100 Subject: [PATCH] Add initGraphQLTada alias type overload --- src/api.ts | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/api.ts b/src/api.ts index 116d8d99..8b80c372 100644 --- a/src/api.ts +++ b/src/api.ts @@ -259,6 +259,33 @@ type configOfSetup = { isMaskingDisabled: Setup['disableMasking'] extends true ? true : false; }; +/** Utility to type-instantiate a `graphql` document function with. + * + * @remarks + * The `initGraphQLTada` type may be used to manually instantiate a type + * as returned by `initGraphQLTada<>()` with the same input type. + * + * @example + * ``` + * import { initGraphQLTada } from 'gql.tada'; + * import type { myIntrospection } from './myIntrospection'; + * + * export const graphql: initGraphQLTada<{ + * introspection: typeof myIntrospection; + * scalars: { + * DateTime: string; + * Json: any; + * }; + * }> = initGraphQLTada(); + * + * const query = graphql(`{ __typename }`); + * ``` + */ +export type initGraphQLTada = GraphQLTadaAPI< + schemaOfSetup, + configOfSetup +>; + /** Setup function to create a typed `graphql` document function with. * * @remarks @@ -285,7 +312,7 @@ type configOfSetup = { * const query = graphql(`{ __typename }`); * ``` */ -function initGraphQLTada() { +export function initGraphQLTada(): initGraphQLTada { type Schema = schemaOfSetup; type Config = configOfSetup; @@ -709,12 +736,9 @@ function unsafe_readResult< return data as any; } -const graphql: GraphQLTadaAPI< - schemaOfSetup, - configOfSetup -> = initGraphQLTada(); +const graphql: initGraphQLTada = initGraphQLTada(); -export { parse, graphql, readFragment, maskFragments, unsafe_readResult, initGraphQLTada }; +export { parse, graphql, readFragment, maskFragments, unsafe_readResult }; export type { setupCache,