Skip to content

Commit

Permalink
Make getVariables tail recursive
Browse files Browse the repository at this point in the history
More correct, as per tail recursion in other
places, but `variables.ts` didn't get much
attention before
  • Loading branch information
kitten committed Feb 3, 2024
1 parent f49842f commit a8628c2
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,32 @@ type unwrapTypeRef<Type, Introspection extends IntrospectionLikeType> = Type ext
? _unwrapTypeRefRec<Type['type'], Introspection>
: null | _unwrapTypeRefRec<Type, Introspection>;

type getVariablesRec<Variables, Introspection extends IntrospectionLikeType> = Variables extends [
infer Variable,
...infer Rest,
]
? (Variable extends { kind: Kind.VARIABLE_DEFINITION; variable: any; type: any }
? Variable extends { defaultValue: undefined; type: { kind: Kind.NON_NULL_TYPE } }
? {
[Name in Variable['variable']['name']['value']]: unwrapTypeRef<
Variable['type'],
Introspection
>;
}
: {
[Name in Variable['variable']['name']['value']]?: unwrapTypeRef<
Variable['type'],
Introspection
>;
}
: {}) &
getVariablesRec<Rest, Introspection>
: {};
type _getVariablesRec<
Variables,
Introspection extends IntrospectionLikeType,
VariablesObject = {},
> = Variables extends [infer Variable, ...infer Rest]
? _getVariablesRec<
Rest,
Introspection,
(Variable extends { kind: Kind.VARIABLE_DEFINITION; variable: any; type: any }
? Variable extends { defaultValue: undefined; type: { kind: Kind.NON_NULL_TYPE } }
? {
[Name in Variable['variable']['name']['value']]: unwrapTypeRef<
Variable['type'],
Introspection
>;
}
: {
[Name in Variable['variable']['name']['value']]?: unwrapTypeRef<
Variable['type'],
Introspection
>;
}
: {}) &
VariablesObject
>
: VariablesObject;

type getVariablesType<
Document extends DocumentNodeLike,
Expand All @@ -96,7 +101,7 @@ type getVariablesType<
kind: Kind.OPERATION_DEFINITION;
variableDefinitions: any;
}
? obj<getVariablesRec<Document['definitions'][0]['variableDefinitions'], Introspection>>
? obj<_getVariablesRec<Document['definitions'][0]['variableDefinitions'], Introspection>>
: {};

export type { getVariablesType };

0 comments on commit a8628c2

Please sign in to comment.