Skip to content

Commit bc1564a

Browse files
committed
wip
1 parent 164fac4 commit bc1564a

File tree

3 files changed

+55
-57
lines changed

3 files changed

+55
-57
lines changed

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

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

5-
// Note: turns out the generated query does NOT have any params :|
6-
// type ParamsType<SRC> = SRC extends [infer Params, infer Result] ? Params : { [K in keyof SRC]: ParamsType<SRC[K]> };
7-
8-
// type PayloadType<SRC, DST> = MapType<SRC, IsPayload<DST>>;
9-
10-
export function createTypedQuery<Z extends ValueTypes['query_root']>(
11-
query: Z,
5+
export function tq<Z extends ValueTypes['query_root']>(
6+
query: Z | ValueTypes['query_root'],
127
): TypedDocumentNode<InputType<GraphQLTypes['query_root'], Z>, {}> {
13-
const zeusQuery = Zeus('query', query);
14-
const gqlQuery = gql(zeusQuery);
8+
return gql(Zeus('query', query));
9+
}
10+
11+
export function tm<Z extends ValueTypes['mutation_root']>(
12+
mutation: Z,
13+
): TypedDocumentNode<InputType<GraphQLTypes['mutation_root'], Z>, {}> {
14+
return gql(Zeus('mutation', mutation));
15+
}
1516

16-
return gqlQuery;
17+
export function ts<Z extends ValueTypes['subscription_root']>(
18+
mutation: Z,
19+
): TypedDocumentNode<InputType<GraphQLTypes['subscription_root'], Z>, {}> {
20+
return gql(Zeus('mutation', mutation));
1721
}
1822

23+
tq({
24+
aggregateBookings: [
25+
{
26+
where: {
27+
bookerName: { _eq: '5' },
28+
},
29+
},
30+
{
31+
aggregate: {
32+
avg: {
33+
nights: true,
34+
},
35+
},
36+
},
37+
],
38+
});
1939
// Example
20-
const userMemberships = createTypedQuery({
40+
const userMemberships = tq({
2141
user: [
2242
{
2343
id: 'x',
@@ -34,3 +54,14 @@ const userMemberships = createTypedQuery({
3454
},
3555
],
3656
});
57+
58+
const mutate = tm({
59+
insertBooking: [
60+
{
61+
object: {},
62+
},
63+
{
64+
bookerName: true,
65+
},
66+
],
67+
});

src/CLI/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
22
import * as yargs from 'yargs';
3-
import { boolean } from 'yargs';
43
import { CLI } from './CLIClass';
54
const args = yargs
65
.usage(

src/plugins/typed-document-node/index.ts

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,40 @@
11
import { OperationType, ParserTree } from 'graphql-js-tree';
22

3-
const pluginApolloOps = ({ queryName, operation }: { queryName: string; operation: OperationType | 'LazyQuery' }) => {
4-
const capitalized = operation[0].toUpperCase() + operation.slice(1);
5-
const zeusOperation = operation === 'LazyQuery' ? OperationType.query : operation;
3+
const createOperationFunction = ({ queryName, operation }: { queryName: string; operation: OperationType }) => {
4+
const firstLetter = operation[0];
65

76
return {
87
queryName,
98
operation,
10-
ts: `export function useTyped${capitalized}<Z extends ValueTypes[O], O extends "${queryName}">(
11-
${operation}: Z | ValueTypes[O],
12-
options?: ${capitalized}HookOptions<InputType<GraphQLTypes[O], Z>>,
13-
operationName?: string,
14-
) {
15-
return use${capitalized}<InputType<GraphQLTypes[O], Z>>(gql(Zeus("${zeusOperation}",${operation}, operationName)), options);
16-
}`,
9+
ts: `export function t${firstLetter}<Z extends ValueTypes["${queryName}"]>(
10+
${operation}: Z | ValueTypes["${queryName}"],
11+
): TypedDocumentNode<InputType<GraphQLTypes["${queryName}"], Z>, {}> {
12+
return gql(Zeus("${operation}", ${operation}));
13+
}
14+
`,
1715
};
1816
};
1917

2018
export const pluginTypedDocumentNode = ({ tree, esModule }: { tree: ParserTree; esModule?: boolean }) => {
2119
const operationNodes = tree.nodes.filter((n) => n.type.operations);
2220
const opsFunctions = operationNodes.flatMap((n) =>
23-
n.type.operations!.map((o) => pluginApolloOps({ queryName: n.name, operation: o })),
21+
n.type.operations!.map((o) => createOperationFunction({ queryName: n.name, operation: o })),
2422
);
25-
for (const [index, o] of opsFunctions.entries()) {
26-
if (o.operation === OperationType.query) {
27-
opsFunctions.splice(index + 1, 0, pluginApolloOps({ queryName: o.queryName, operation: 'LazyQuery' }));
28-
break;
29-
}
30-
}
31-
const o = opsFunctions.reduce<Pick<ReturnType<typeof pluginApolloOps>, 'ts'>>(
23+
24+
const o = opsFunctions.reduce(
3225
(a, b) => {
3326
a.ts = [a.ts, b.ts].join('\n');
3427
return a;
3528
},
3629
{ ts: '' },
3730
);
38-
const capitalizedOps = opsFunctions.map((o) => o.operation[0].toUpperCase() + o.operation.slice(1));
39-
const jsDefsImports: string[] = [];
40-
if (capitalizedOps.includes('LazyQuery')) {
41-
jsDefsImports.push('QueryTuple');
42-
}
43-
if (capitalizedOps.includes('Query')) {
44-
jsDefsImports.push('QueryResult');
45-
}
46-
if (capitalizedOps.includes('Mutation')) {
47-
jsDefsImports.push('MutationTuple');
48-
}
31+
4932
return {
5033
ts: `/* eslint-disable */
5134
5235
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
5336
import gql from 'graphql-tag';
54-
import { GraphQLTypes, InputType, Selectors, ValueTypes, Zeus } from './index${esModule ? '.js' : ''}';
55-
56-
export const constructQuery = <T extends ValueTypes['Query']>(q: T) => {
57-
const gqlString = Zeus.query(q);
58-
const selector = Selectors.query(q);
59-
type InferredResponseType = InputType<GraphQLTypes['Query'], typeof selector>;
60-
return gql(gqlString) as TypedDocumentNode<InferredResponseType, {}>;
61-
};
62-
63-
const drawCardDocument = constructQuery({
64-
drawCard: {
65-
Attack: true,
66-
Children: true,
67-
id: true,
68-
},
69-
});
37+
import { GraphQLTypes, InputType, ValueTypes, Zeus } from './index';
7038
7139
${o.ts}
7240
`,

0 commit comments

Comments
 (0)