Skip to content

Commit 7dddd5b

Browse files
committed
WIPWIP
1 parent 3aa2175 commit 7dddd5b

File tree

3 files changed

+145
-136
lines changed

3 files changed

+145
-136
lines changed

examples/typescript-node-big-schema/src/zeus/typedDocumentNode.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
22
import gql from 'graphql-tag';
3-
import { GraphQLTypes, InputType, ValueTypes, Zeus } from './index';
3+
import { GraphQLTypes, InputType, ValueTypes, Zeus, $ } from './index';
44

5-
export function tq<Z extends ValueTypes['query_root']>(
6-
query: Z | ValueTypes['query_root'],
7-
): TypedDocumentNode<InputType<GraphQLTypes['query_root'], Z>, {}> {
8-
return gql(Zeus('query', query));
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>;
16+
};
17+
18+
type X<K extends string> = { [Key in K]: Variable<any, K> };
19+
20+
type Variable<T, Name extends string> = {
21+
__zeus_name: Name;
22+
__zeus_type: T;
23+
};
24+
25+
type VariablizedInput<T> = T extends string | number | Array<any>
26+
? T | Variable<T, any>
27+
: T | Variable<T, any> | { [K in keyof T]: VariablizedInput<T[K]> };
28+
29+
type VariablizedQuery<T> = T extends [infer Input, infer Output]
30+
? [VariablizedInput<Input>, VariablizedQuery<Output>]
31+
: { [K in keyof T]: VariablizedQuery<T[K]> };
32+
33+
type ConnValue = ValueTypes['connection'];
34+
type Test = VariablizedQuery<ConnValue>;
35+
export function tq<ResultType extends ValueTypes['query_root'], VariablesType>(
36+
query: VariablizedQuery<ResultType>,
37+
): TypedDocumentNode<InputType<GraphQLTypes['query_root'], ResultType>, VariablesType> {
38+
return gql(Zeus('query', query as any));
939
}
1040

1141
export function tm<Z extends ValueTypes['mutation_root']>(
@@ -24,7 +54,7 @@ tq({
2454
aggregateBookings: [
2555
{
2656
where: {
27-
bookerName: { _eq: '5' },
57+
bookerName: { _eq: $$('bookerName') },
2858
},
2959
},
3060
{
@@ -36,16 +66,17 @@ tq({
3666
},
3767
],
3868
});
69+
3970
// Example
4071
const userMemberships = tq({
4172
user: [
4273
{
43-
id: 'x',
74+
id: $$('id'),
4475
},
4576
{
4677
memberships: [
4778
{
48-
limit: 5,
79+
limit: $$('limit'),
4980
},
5081
{
5182
role: true,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
2+
import gql from 'graphql-tag';
3+
import { GraphQLTypes, InputType, ValueTypes, Zeus, $ } from './index';
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>;
16+
};
17+
18+
type X<K extends string> = { [Key in K]: Variable<any, K> };
19+
20+
type Variable<T, Name extends string> = {
21+
__zeus_name: Name;
22+
__zeus_type: T;
23+
};
24+
25+
type VariablizedInput<T> = T extends string | number | Array<any>
26+
? T | Variable<T, any>
27+
: T | Variable<T, any> | { [K in keyof T]: VariablizedInput<T[K]> };
28+
29+
type VariablizedQuery<T> = T extends [infer Input, infer Output]
30+
? [VariablizedInput<Input>, VariablizedQuery<Output>]
31+
: { [K in keyof T]: VariablizedQuery<T[K]> };
32+
33+
type ExtractVariables<C> = C extends Variable<infer VType, infer VName>
34+
? { [key in VName]: VType }
35+
: { [K in keyof C]: ExtractVariables<C[K]> };
36+
37+
export function tq<ResultType extends VariablizedQuery<ValueTypes['Query']>>(
38+
query: ResultType,
39+
): TypedDocumentNode<InputType<GraphQLTypes['Query'], ResultType>, VariablesType> {
40+
return gql(Zeus('query', query as any));
41+
}
42+
43+
export function tm<Z extends ValueTypes['Mutation']>(
44+
mutation: Z,
45+
): TypedDocumentNode<InputType<GraphQLTypes['Mutation'], Z>, {}> {
46+
return gql(Zeus('mutation', mutation));
47+
}
48+
49+
export function ts<Z extends ValueTypes['Subscription']>(
50+
mutation: Z,
51+
): TypedDocumentNode<InputType<GraphQLTypes['Subscription'], Z>, {}> {
52+
return gql(Zeus('mutation', mutation));
53+
}
54+
55+
const q = tq({
56+
cardById: [
57+
{
58+
cardId: $$('test'),
59+
},
60+
{
61+
Attack: true,
62+
Defense: true,
63+
},
64+
],
65+
});
66+
67+
// Example
68+
// const userMemberships = tq({
69+
// user: [
70+
// {
71+
// id: $$('id'),
72+
// },
73+
// {
74+
// memberships: [
75+
// {
76+
// limit: $$('limit'),
77+
// },
78+
// {
79+
// role: true,
80+
// },
81+
// ],
82+
// },
83+
// ],
84+
// });
85+
86+
// const mutate = tm({
87+
// insertBooking: [
88+
// {
89+
// object: {},
90+
// },
91+
// {
92+
// bookerName: true,
93+
// },
94+
// ],
95+
// });

0 commit comments

Comments
 (0)