|
1 | 1 | import { OperationType, ParserTree } from 'graphql-js-tree'; |
2 | 2 |
|
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]; |
6 | 5 |
|
7 | 6 | return { |
8 | 7 | queryName, |
9 | 8 | 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 | +`, |
17 | 15 | }; |
18 | 16 | }; |
19 | 17 |
|
20 | 18 | export const pluginTypedDocumentNode = ({ tree, esModule }: { tree: ParserTree; esModule?: boolean }) => { |
21 | 19 | const operationNodes = tree.nodes.filter((n) => n.type.operations); |
22 | 20 | 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 })), |
24 | 22 | ); |
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( |
32 | 25 | (a, b) => { |
33 | 26 | a.ts = [a.ts, b.ts].join('\n'); |
34 | 27 | return a; |
35 | 28 | }, |
36 | 29 | { ts: '' }, |
37 | 30 | ); |
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 | + |
49 | 32 | return { |
50 | 33 | ts: `/* eslint-disable */ |
51 | 34 |
|
52 | 35 | import type { TypedDocumentNode } from '@graphql-typed-document-node/core'; |
53 | 36 | 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'; |
70 | 38 |
|
71 | 39 | ${o.ts} |
72 | 40 | `, |
|
0 commit comments