1
1
import type { TypedDocumentNode } from '@graphql-typed-document-node/core' ;
2
2
import gql from 'graphql-tag' ;
3
- import { GraphQLTypes , InputType , ValueTypes , Zeus , $ } from './index' ;
3
+ import { GraphQLTypes , InputType , ValueTypes , Zeus } from './index' ;
4
4
5
- // const $$ = new Proxy(
6
- // {},
7
- // {
8
- // get(target, propName, receiver) {
9
- // return 'ZEUS_VAR$' + propName.toString();
10
- // },
11
- // },
12
- // ) as any as X
13
-
14
- const $$ = < Name extends string > ( name : Name ) => {
15
- return ( 'ZEUS_VAR$' + name ) as any as Variable < any , Name > ;
5
+ const $$ = < Type , Name extends string > ( name : Name ) => {
6
+ return ( 'ZEUS_VAR$' + name ) as any as Variable < Type , Name > ;
16
7
} ;
17
8
18
- type X < K extends string > = { [ Key in K ] : Variable < any , K > } ;
19
-
20
9
type Variable < T , Name extends string > = {
21
10
__zeus_name : Name ;
22
11
__zeus_type : T ;
@@ -30,13 +19,26 @@ type VariablizedQuery<T> = T extends [infer Input, infer Output]
30
19
? [ VariablizedInput < Input > , VariablizedQuery < Output > ]
31
20
: { [ K in keyof T ] : VariablizedQuery < T [ K ] > } ;
32
21
33
- type ExtractVariables < C > = C extends Variable < infer VType , infer VName >
22
+ type ExtractVariables < Query > = Query extends Variable < infer VType , infer VName >
34
23
? { [ key in VName ] : VType }
35
- : { [ K in keyof C ] : ExtractVariables < C [ K ] > } ;
24
+ : Query extends [ infer Inputs , infer Outputs ]
25
+ ? Intersectionize < { inputs : ExtractVariables < Inputs > ; outputs : ExtractVariables < Outputs > } >
26
+ : Query extends string | number | boolean
27
+ ? { }
28
+ : Intersectionize < { [ K in keyof Query ] : ExtractVariables < Query [ K ] > } > ;
29
+
30
+ type Intersectionize < ObjectKeys > = UnionToIntersection < ObjectKeys [ keyof ObjectKeys ] > ;
31
+
32
+ type UnionToIntersection < U > = ( U extends any ? ( k : U ) => void : never ) extends ( k : infer I ) => void ? I : never ;
33
+
34
+ // Unit testing:
35
+ // type TestExtractor = ExtractVariables<{
36
+ // cardById: [{ cardId: Variable<any, 'test'> }, { attack: true; defense: true }];
37
+ // }>;
36
38
37
39
export function tq < ResultType extends VariablizedQuery < ValueTypes [ 'Query' ] > > (
38
40
query : ResultType ,
39
- ) : TypedDocumentNode < InputType < GraphQLTypes [ 'Query' ] , ResultType > , VariablesType > {
41
+ ) : TypedDocumentNode < InputType < GraphQLTypes [ 'Query' ] , ResultType > , ExtractVariables < ResultType > > {
40
42
return gql ( Zeus ( 'query' , query as any ) ) ;
41
43
}
42
44
0 commit comments