11import type { TypedDocumentNode } from '@graphql-typed-document-node/core' ;
22import gql from 'graphql-tag' ;
3- import { GraphQLTypes , InputType , ValueTypes , Zeus , $ } from './index' ;
3+ import { GraphQLTypes , InputType , ValueTypes , Zeus } from './index' ;
44
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 > ;
167} ;
178
18- type X < K extends string > = { [ Key in K ] : Variable < any , K > } ;
19-
209type Variable < T , Name extends string > = {
2110 __zeus_name : Name ;
2211 __zeus_type : T ;
@@ -30,13 +19,26 @@ type VariablizedQuery<T> = T extends [infer Input, infer Output]
3019 ? [ VariablizedInput < Input > , VariablizedQuery < Output > ]
3120 : { [ K in keyof T ] : VariablizedQuery < T [ K ] > } ;
3221
33- type ExtractVariables < C > = C extends Variable < infer VType , infer VName >
22+ type ExtractVariables < Query > = Query extends Variable < infer VType , infer VName >
3423 ? { [ 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+ // }>;
3638
3739export function tq < ResultType extends VariablizedQuery < ValueTypes [ 'Query' ] > > (
3840 query : ResultType ,
39- ) : TypedDocumentNode < InputType < GraphQLTypes [ 'Query' ] , ResultType > , VariablesType > {
41+ ) : TypedDocumentNode < InputType < GraphQLTypes [ 'Query' ] , ResultType > , ExtractVariables < ResultType > > {
4042 return gql ( Zeus ( 'query' , query as any ) ) ;
4143}
4244
0 commit comments