Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TypedDocumentNode support (with type-safe variables) #275

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIIPWIPWIP
  • Loading branch information
spion committed Apr 21, 2022
commit d7d122e8ef3cbed329bafedcacb003e481235e8e
Original file line number Diff line number Diff line change
@@ -2,14 +2,14 @@ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
import gql from 'graphql-tag';
import { GraphQLTypes, InputType, ValueTypes, Zeus, $ } from './index';

// const $$ = new Proxy(
// {},
// {
// get(target, propName, receiver) {
// return 'ZEUS_VAR$' + propName.toString();
// },
// },
// ) as any as X
const $$ = new Proxy(
{},
{
get(target, propName, receiver) {
return 'ZEUS_VAR$' + propName.toString();
},
},
);

const $$ = <Name extends string>(name: Name) => {
return ('ZEUS_VAR$' + name) as any as Variable<any, Name>;
36 changes: 19 additions & 17 deletions examples/typescript-node/src/zeus/typedDocumentNode.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
import gql from 'graphql-tag';
import { GraphQLTypes, InputType, ValueTypes, Zeus, $ } from './index';
import { GraphQLTypes, InputType, ValueTypes, Zeus } from './index';

// const $$ = new Proxy(
// {},
// {
// get(target, propName, receiver) {
// return 'ZEUS_VAR$' + propName.toString();
// },
// },
// ) as any as X

const $$ = <Name extends string>(name: Name) => {
return ('ZEUS_VAR$' + name) as any as Variable<any, Name>;
const $$ = <Type, Name extends string>(name: Name) => {
return ('ZEUS_VAR$' + name) as any as Variable<Type, Name>;
};

type X<K extends string> = { [Key in K]: Variable<any, K> };

type Variable<T, Name extends string> = {
__zeus_name: Name;
__zeus_type: T;
@@ -30,13 +19,26 @@ type VariablizedQuery<T> = T extends [infer Input, infer Output]
? [VariablizedInput<Input>, VariablizedQuery<Output>]
: { [K in keyof T]: VariablizedQuery<T[K]> };

type ExtractVariables<C> = C extends Variable<infer VType, infer VName>
type ExtractVariables<Query> = Query extends Variable<infer VType, infer VName>
? { [key in VName]: VType }
: { [K in keyof C]: ExtractVariables<C[K]> };
: Query extends [infer Inputs, infer Outputs]
? Intersectionize<{ inputs: ExtractVariables<Inputs>; outputs: ExtractVariables<Outputs> }>
: Query extends string | number | boolean
? {}
: Intersectionize<{ [K in keyof Query]: ExtractVariables<Query[K]> }>;

type Intersectionize<ObjectKeys> = UnionToIntersection<ObjectKeys[keyof ObjectKeys]>;

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;

// Unit testing:
// type TestExtractor = ExtractVariables<{
// cardById: [{ cardId: Variable<any, 'test'> }, { attack: true; defense: true }];
// }>;

export function tq<ResultType extends VariablizedQuery<ValueTypes['Query']>>(
query: ResultType,
): TypedDocumentNode<InputType<GraphQLTypes['Query'], ResultType>, VariablesType> {
): TypedDocumentNode<InputType<GraphQLTypes['Query'], ResultType>, ExtractVariables<ResultType>> {
return gql(Zeus('query', query as any));
}