Skip to content

Commit

Permalink
fix: Add missing type alias for initGraphQLTada (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Aug 22, 2024
1 parent a9bf8b8 commit a5d252f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-hairs-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gql.tada': patch
---

Update internal type signature of `initGraphQLTada<>()` function and include `initGraphQLTada<Setup>` alias type. This alias type makes it more intuitive to declare types the return type of `initGraphQLTada()`, since it mirrors the same name.
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 a5d252f

Please sign in to comment.