Skip to content

Commit

Permalink
chore: Reformat selection expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Jan 22, 2024
1 parent 2601bd8 commit 11e2481
Showing 1 changed file with 40 additions and 41 deletions.
81 changes: 40 additions & 41 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type ObjectLikeType = {

type _unwrapTypeRec<
Type extends IntrospectionTypeRef,
SelectionSet extends { kind: Kind.SELECTION_SET } | undefined,
SelectionSet,
Introspection extends IntrospectionLikeType,
Fragments extends { [name: string]: any },
> = Type extends IntrospectionNonNullTypeRef
Expand All @@ -49,7 +49,7 @@ type _unwrapTypeRec<

type unwrapType<
Type extends IntrospectionTypeRef,
SelectionSet extends { kind: Kind.SELECTION_SET } | undefined,
SelectionSet,
Introspection extends IntrospectionLikeType,
Fragments extends { [name: string]: any },
TypeDirective = void,
Expand All @@ -61,25 +61,29 @@ type unwrapType<
? _unwrapTypeRec<Type, SelectionSet, Introspection, Fragments>
: null | _unwrapTypeRec<Type, SelectionSet, Introspection, Fragments>;

type getTypeDirective<Directives extends readonly unknown[] | undefined> =
Directives extends readonly [infer Directive, ...infer Rest]
? Directive extends { kind: Kind.DIRECTIVE; name: any }
? Directive['name']['value'] extends 'required' | '_required'
? 'required'
: Directive['name']['value'] extends 'optional' | '_optional'
? 'optional'
: getTypeDirective<Rest>
: getTypeDirective<Rest>
: void;

type isOptionalRec<Directives extends readonly unknown[] | undefined> =
Directives extends readonly [infer Directive, ...infer Rest]
? Directive extends { kind: Kind.DIRECTIVE; name: any }
? Directive['name']['value'] extends 'include' | 'skip' | 'defer'
? true
: isOptionalRec<Rest>
type getTypeDirective<Directives extends readonly any[] | undefined> = Directives extends readonly [
infer Directive,
...infer Rest,
]
? Directive extends { kind: Kind.DIRECTIVE; name: any }
? Directive['name']['value'] extends 'required' | '_required'
? 'required'
: Directive['name']['value'] extends 'optional' | '_optional'
? 'optional'
: getTypeDirective<Rest>
: getTypeDirective<Rest>
: void;

type isOptionalRec<Directives extends readonly any[] | undefined> = Directives extends readonly [
infer Directive,
...infer Rest,
]
? Directive extends { kind: Kind.DIRECTIVE; name: any }
? Directive['name']['value'] extends 'include' | 'skip' | 'defer'
? true
: isOptionalRec<Rest>
: false;
: isOptionalRec<Rest>
: false;

type getFieldAlias<Node extends FieldNode> = Node['alias'] extends undefined
? Node['name']['value']
Expand Down Expand Up @@ -133,10 +137,24 @@ type getSelection<
Type extends ObjectLikeType,
Introspection extends IntrospectionLikeType,
Fragments extends { [name: string]: any },
> = obj<getPossibleTypesSelection<Selections, Type, Introspection, Fragments>>;
> = obj<
Type extends { kind: 'UNION' | 'INTERFACE'; possibleTypes: any }
? objValues<{
[PossibleType in Type['possibleTypes']]: _getPossibleTypeSelectionRec<
Selections,
PossibleType,
Type,
Introspection,
Fragments
>;
}>
: Type extends { kind: 'OBJECT'; name: any }
? _getPossibleTypeSelectionRec<Selections, Type['name'], Type, Introspection, Fragments>
: {}
>;

type _getPossibleTypeSelectionRec<
Selections extends readonly unknown[],
Selections extends readonly any[],
PossibleType extends string,
Type extends ObjectLikeType,
Introspection extends IntrospectionLikeType,
Expand Down Expand Up @@ -181,25 +199,6 @@ type _getPossibleTypeSelectionRec<
_getPossibleTypeSelectionRec<Rest, PossibleType, Type, Introspection, Fragments>
: {};

type getPossibleTypesSelection<
Selections extends readonly unknown[],
Type extends ObjectLikeType,
Introspection extends IntrospectionLikeType,
Fragments extends { [name: string]: any },
> = Type extends { kind: 'UNION' | 'INTERFACE'; possibleTypes: any }
? objValues<{
[PossibleType in Type['possibleTypes']]: _getPossibleTypeSelectionRec<
Selections,
PossibleType,
Type,
Introspection,
Fragments
>;
}>
: Type extends { kind: 'OBJECT'; name: any }
? _getPossibleTypeSelectionRec<Selections, Type['name'], Type, Introspection, Fragments>
: {};

type getOperationSelectionType<
Definition,
Introspection extends IntrospectionLikeType,
Expand Down

0 comments on commit 11e2481

Please sign in to comment.