|
| 1 | +import { OperationType, ParserTree } from 'graphql-js-tree'; |
| 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; |
| 6 | + |
| 7 | + return { |
| 8 | + queryName, |
| 9 | + 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 | +}`, |
| 17 | + }; |
| 18 | +}; |
| 19 | + |
| 20 | +export const pluginTypedDocumentNode = ({ tree, esModule }: { tree: ParserTree; esModule?: boolean }) => { |
| 21 | + const operationNodes = tree.nodes.filter((n) => n.type.operations); |
| 22 | + const opsFunctions = operationNodes.flatMap((n) => |
| 23 | + n.type.operations!.map((o) => pluginApolloOps({ queryName: n.name, operation: o })), |
| 24 | + ); |
| 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'>>( |
| 32 | + (a, b) => { |
| 33 | + a.ts = [a.ts, b.ts].join('\n'); |
| 34 | + return a; |
| 35 | + }, |
| 36 | + { ts: '' }, |
| 37 | + ); |
| 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 | + } |
| 49 | + return { |
| 50 | + ts: `/* eslint-disable */ |
| 51 | +
|
| 52 | +import type { TypedDocumentNode } from '@graphql-typed-document-node/core'; |
| 53 | +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 | +}); |
| 70 | +
|
| 71 | +${o.ts} |
| 72 | +`, |
| 73 | + }; |
| 74 | +}; |
0 commit comments