-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: Treat nullable types as optional ones in GraphQL variables
- Loading branch information
Snowflyt
committed
Mar 21, 2024
1 parent
2afb322
commit 8d1605a
Showing
5 changed files
with
46 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,60 @@ | ||
import type { Merge, StringKeyOf, StringLiteral } from './common'; | ||
import type { GraphQLEnum, GraphQLList, GraphQLNonNull } from './graphql-types'; | ||
|
||
type TryResolve<T extends StringKeyOf<$>, $> = $[T] extends | ||
| StringLiteral | ||
| Record<string, StringLiteral> | ||
? Parse<$[T], $> | ||
type TryResolve< | ||
T extends StringKeyOf<$>, | ||
$, | ||
TFlags extends { treatNullableTypeAsOptional: boolean } = { treatNullableTypeAsOptional: false }, | ||
> = $[T] extends StringLiteral | Record<string, StringLiteral> | ||
? Parse<$[T], $, TFlags> | ||
: $[T] extends GraphQLEnum<infer S> | ||
? S | ||
: $[T]; | ||
|
||
export type Parse<T, $> = T extends Record<string, StringLiteral> | ||
? Merge< | ||
{ [P in keyof T as P extends `${string}?` ? never : P]: Parse<T[P], $> }, | ||
{ [P in keyof T as P extends `${infer K}?` ? K : never]?: Parse<T[P], $> } | ||
> | ||
export type Parse< | ||
T, | ||
$, | ||
TFlags extends { treatNullableTypeAsOptional: boolean } = { treatNullableTypeAsOptional: false }, | ||
> = T extends Record<string, StringLiteral> | ||
? TFlags['treatNullableTypeAsOptional'] extends true | ||
? Merge< | ||
{ | ||
[P in keyof T as P extends `${string}?` | ||
? never | ||
: T[P] extends `${string}!` | ||
? P | ||
: never]: Parse<T[P], $, TFlags>; | ||
}, | ||
{ | ||
[P in keyof T as P extends `${infer K}?` | ||
? K | ||
: T[P] extends `${string}!` | ||
? never | ||
: P]?: Parse<T[P], $, TFlags>; | ||
} | ||
> | ||
: Merge< | ||
{ [P in keyof T as P extends `${string}?` ? never : P]: Parse<T[P], $, TFlags> }, | ||
{ [P in keyof T as P extends `${infer K}?` ? K : never]?: Parse<T[P], $, TFlags> } | ||
> | ||
: T extends GraphQLNonNull<infer U extends StringKeyOf<$>> | ||
? // HACK: I use this ugly `infer R` instead of just `Exclude<..., null>` because the latter will | ||
// cause a infinite loop when compiling—I don't know why, but it works. | ||
TryResolve<U, $> extends infer R | ||
TryResolve<U, $, TFlags> extends infer R | ||
? R extends U | null | ||
? U | ||
: R | ||
: never | ||
: T extends StringKeyOf<$> | ||
? TryResolve<T, $> | null | ||
? TryResolve<T, $, TFlags> | null | ||
: T extends GraphQLNonNull<GraphQLList<infer U>> | ||
? // HACK: I use this ugly `infer R` instead of just `Exclude<..., null>` because the latter will | ||
// cause a infinite loop when compiling—I don't know why, but it works. | ||
Parse<U, $>[] extends infer R | ||
Parse<U, $, TFlags>[] extends infer R | ||
? R extends U | null | ||
? U | ||
: R | ||
: never | ||
: T extends GraphQLList<infer U> | ||
? Parse<U, $>[] | null | ||
? Parse<U, $, TFlags>[] | null | ||
: T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters