-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: type safety for gql perms (#9798)
Signed-off-by: Matt Krick <matt.krick@gmail.com>
- Loading branch information
Showing
5 changed files
with
48 additions
and
9 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
34 changes: 32 additions & 2 deletions
34
packages/server/graphql/public/rules/getResolverDotPath.ts
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,9 +1,39 @@ | ||
import {FirstParam} from '../../../../client/types/generics' | ||
import {Resolvers} from '../resolverTypes' | ||
|
||
export const getResolverDotPath = ( | ||
dotPath: ResolverDotPath, | ||
dotPath: `${'source' | 'args'}.${string}`, | ||
source: Record<string, any>, | ||
args: Record<string, any> | ||
) => { | ||
return dotPath.split('.').reduce((val: any, key) => val?.[key], {source, args}) | ||
} | ||
|
||
export type ResolverDotPath = `source.${string}` | `args.${string}` | ||
type SecondParam<T> = T extends (arg1: any, arg2: infer A, ...args: any[]) => any ? A : never | ||
|
||
type ParseParent<T> = T extends `${infer Parent extends string}.${string}` ? Parent : never | ||
type ParseChild<T> = T extends `${string}.${infer Child extends string}` ? Child : never | ||
|
||
type ExtractTypeof<T extends keyof Resolvers> = '__isTypeOf' extends keyof NonNullable<Resolvers[T]> | ||
? NonNullable<Resolvers[T]>['__isTypeOf'] | ||
: never | ||
type ExtractParent<T extends keyof Resolvers> = FirstParam<NonNullable<ExtractTypeof<T>>> | ||
|
||
type Source<T> = | ||
ParseParent<T> extends keyof Resolvers | ||
? ExtractParent<ParseParent<T>> extends never | ||
? never | ||
: keyof ExtractParent<ParseParent<T>> & string | ||
: never | ||
|
||
type ExtractChild<TOp, TChild extends string> = TChild extends keyof TOp | ||
? NonNullable<TOp[TChild]> | ||
: never | ||
|
||
type Arg<T> = | ||
ParseParent<T> extends keyof Resolvers | ||
? keyof SecondParam<ExtractChild<NonNullable<Resolvers[ParseParent<T>]>, ParseChild<T>>> & | ||
string | ||
: never | ||
|
||
export type ResolverDotPath<T> = `source.${Source<T>}` | `args.${Arg<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
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