Skip to content

Commit

Permalink
Add initGraphQLTada alias type overload
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Aug 22, 2024
1 parent a9bf8b8 commit 208c0a1
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,33 @@ type configOfSetup<Setup extends AbstractSetupSchema> = {
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<Setup extends AbstractSetupSchema> = GraphQLTadaAPI<
schemaOfSetup<Setup>,
configOfSetup<Setup>
>;

/** Setup function to create a typed `graphql` document function with.
*
* @remarks
Expand All @@ -285,7 +312,7 @@ type configOfSetup<Setup extends AbstractSetupSchema> = {
* const query = graphql(`{ __typename }`);
* ```
*/
function initGraphQLTada<const Setup extends AbstractSetupSchema>() {
export function initGraphQLTada<const Setup extends AbstractSetupSchema>(): initGraphQLTada<Setup> {
type Schema = schemaOfSetup<Setup>;
type Config = configOfSetup<Setup>;

Expand Down Expand Up @@ -709,12 +736,9 @@ function unsafe_readResult<
return data as any;
}

const graphql: GraphQLTadaAPI<
schemaOfSetup<setupSchema>,
configOfSetup<setupSchema>
> = initGraphQLTada();
const graphql: initGraphQLTada<setupSchema> = initGraphQLTada();

export { parse, graphql, readFragment, maskFragments, unsafe_readResult, initGraphQLTada };
export { parse, graphql, readFragment, maskFragments, unsafe_readResult };

export type {
setupCache,
Expand Down

0 comments on commit 208c0a1

Please # to comment.