Skip to content

Commit

Permalink
Add DocumentNodeLike type
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Jan 10, 2024
1 parent a985f12 commit 479632a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parse as _parse } from '@0no-co/graphql.web';
import type { stringLiteral } from './utils';
import type { parseDocument } from './parser';

function parse<const Input extends stringLiteral<Input>>(input: Input): parseDocument<Input> {
function parse<const In extends stringLiteral<In>>(input: In): parseDocument<In> {
return _parse(input) as any;
}

Expand Down
6 changes: 4 additions & 2 deletions src/fragments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Kind } from '@0no-co/graphql.web';
import type { DocumentNodeLike } from './parser';

type _getFragmentMapRec<Definitions> = Definitions extends readonly [
infer Definition,
Expand All @@ -10,5 +11,6 @@ type _getFragmentMapRec<Definitions> = Definitions extends readonly [
_getFragmentMapRec<Rest>
: {};

export type getFragmentMap<Document extends { kind: Kind.DOCUMENT; definitions: any[] }> =
_getFragmentMapRec<Document['definitions']>;
export type getFragmentMap<Document extends DocumentNodeLike> = _getFragmentMapRec<
Document['definitions']
>;
5 changes: 5 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,9 @@ type parseType<In> = takeType<skipIgnored<In>> extends [infer Node, string] ? No

type parseOperation<In> = TakeOperation<skipIgnored<In>> extends [infer Node, string] ? Node : void;

export type DocumentNodeLike = {
kind: Kind.DOCUMENT;
definitions: any[];
};

export type { parseConstValue, parseOperation, parseDocument, parseValue, parseType };
5 changes: 3 additions & 2 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import type { tada } from './namespace';
import type { obj, objValues } from './utils';
import type { getFragmentMap } from './fragments';
import type { DocumentNodeLike } from './parser';

import type {
IntrospectionField,
Expand Down Expand Up @@ -217,7 +218,7 @@ type getDefinitionSelectionRec<
: {};

type getDocumentType<
Document extends { kind: Kind.DOCUMENT; definitions: any[] },
Document extends DocumentNodeLike,
Introspection extends IntrospectionLikeType,
Fragments extends { [name: string]: any } = {},
> = getDefinitionSelectionRec<
Expand All @@ -227,7 +228,7 @@ type getDocumentType<
>;

type getFragmentType<
Document extends { kind: Kind.DOCUMENT; definitions: any[] },
Document extends DocumentNodeLike,
Introspection extends IntrospectionLikeType,
Fragments extends { [name: string]: any } = {},
> = Document['definitions'][0] extends {
Expand Down

0 comments on commit 479632a

Please sign in to comment.