diff --git a/src/selection.ts b/src/selection.ts index 55c1ee8e..72d6d435 100644 --- a/src/selection.ts +++ b/src/selection.ts @@ -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 @@ -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, @@ -61,25 +61,29 @@ type unwrapType< ? _unwrapTypeRec : null | _unwrapTypeRec; -type getTypeDirective = - 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 - : getTypeDirective - : void; - -type isOptionalRec = - Directives extends readonly [infer Directive, ...infer Rest] - ? Directive extends { kind: Kind.DIRECTIVE; name: any } - ? Directive['name']['value'] extends 'include' | 'skip' | 'defer' - ? true - : isOptionalRec +type getTypeDirective = 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 + : getTypeDirective + : void; + +type isOptionalRec = Directives extends readonly [ + infer Directive, + ...infer Rest, +] + ? Directive extends { kind: Kind.DIRECTIVE; name: any } + ? Directive['name']['value'] extends 'include' | 'skip' | 'defer' + ? true : isOptionalRec - : false; + : isOptionalRec + : false; type getFieldAlias = Node['alias'] extends undefined ? Node['name']['value'] @@ -133,10 +137,24 @@ type getSelection< Type extends ObjectLikeType, Introspection extends IntrospectionLikeType, Fragments extends { [name: string]: any }, -> = obj>; +> = 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 + : {} +>; type _getPossibleTypeSelectionRec< - Selections extends readonly unknown[], + Selections extends readonly any[], PossibleType extends string, Type extends ObjectLikeType, Introspection extends IntrospectionLikeType, @@ -181,25 +199,6 @@ type _getPossibleTypeSelectionRec< _getPossibleTypeSelectionRec : {}; -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 - : {}; - type getOperationSelectionType< Definition, Introspection extends IntrospectionLikeType,