|
| 1 | +import { cleanSplit, mapEntries } from '@seedcompany/common'; |
| 2 | +import { adapter } from 'edgedb'; |
| 3 | +import { $ as $$ } from 'execa'; |
| 4 | +import { readFile } from 'fs/promises'; |
| 5 | +import { Directory, Node, SyntaxKind } from 'ts-morph'; |
| 6 | +import { hydratorsNeeded, injectHydrators } from './inject-hydrators'; |
| 7 | +import { GeneratorParams, toFqn } from './util'; |
| 8 | + |
| 9 | +interface Hydrator { |
| 10 | + fqn: string; |
| 11 | + type: string; |
| 12 | + query: string; |
| 13 | + fields: string; |
| 14 | + source: string; |
| 15 | + dependencies: Set<string>; |
| 16 | +} |
| 17 | +export type HydratorMap = ReadonlyMap<string, Hydrator>; |
| 18 | + |
| 19 | +export async function findHydrationShapes({ root }: GeneratorParams) { |
| 20 | + const queries = await Promise.all([ |
| 21 | + findInQueryFiles(root), |
| 22 | + findInInlineQueries(root), |
| 23 | + ]); |
| 24 | + |
| 25 | + const unsorted = mapEntries(queries.flat(), ({ query, source }, { SKIP }) => { |
| 26 | + const matches = query.match(RE_SELECT_NAME_AND_FIELDS); |
| 27 | + if (!matches) { |
| 28 | + return SKIP; |
| 29 | + } |
| 30 | + const [_, type] = matches; |
| 31 | + const fqn = toFqn(type); |
| 32 | + const dependencies = hydratorsNeeded(query); |
| 33 | + const hydrator: Hydrator = { |
| 34 | + fqn, |
| 35 | + type, |
| 36 | + query, |
| 37 | + source, |
| 38 | + dependencies, |
| 39 | + fields: '', |
| 40 | + }; |
| 41 | + return [fqn, hydrator]; |
| 42 | + }).asMap; |
| 43 | + |
| 44 | + const hydrators = topoSort(unsorted); |
| 45 | + |
| 46 | + for (const hydrator of hydrators.values()) { |
| 47 | + hydrator.query = injectHydrators(hydrator.query, hydrators); |
| 48 | + hydrator.fields = hydrator.query.match(RE_SELECT_NAME_AND_FIELDS)![2]; |
| 49 | + } |
| 50 | + |
| 51 | + return hydrators; |
| 52 | +} |
| 53 | + |
| 54 | +function topoSort(map: HydratorMap): HydratorMap { |
| 55 | + const sorted = new Map<string, Hydrator>(); |
| 56 | + const visiting = new Set<string>(); |
| 57 | + |
| 58 | + const visit = (hydrator: Hydrator) => { |
| 59 | + if (visiting.has(hydrator.fqn)) { |
| 60 | + const last = Array.from(visiting).at(-1)!; |
| 61 | + throw new Error( |
| 62 | + `Circular dependency involving ${hydrator.type} and ${last}`, |
| 63 | + ); |
| 64 | + } |
| 65 | + visiting.add(hydrator.fqn); |
| 66 | + for (const dep of hydrator.dependencies) { |
| 67 | + const depHydrator = map.get(dep); |
| 68 | + if (!depHydrator) { |
| 69 | + throw new Error(`Hydrator ${dep} referenced but not defined`); |
| 70 | + } |
| 71 | + visit(depHydrator); |
| 72 | + } |
| 73 | + sorted.set(hydrator.fqn, hydrator); |
| 74 | + visiting.delete(hydrator.fqn); |
| 75 | + }; |
| 76 | + |
| 77 | + for (const type of map.values()) { |
| 78 | + visit(type); |
| 79 | + } |
| 80 | + |
| 81 | + return sorted; |
| 82 | +} |
| 83 | + |
| 84 | +async function findInQueryFiles(root: Directory) { |
| 85 | + const grepForShortList = await $$({ |
| 86 | + reject: false, |
| 87 | + cwd: root.getPath(), |
| 88 | + })`find src -type f -name hydrate*.edgeql`; |
| 89 | + const shortList = grepForShortList.stdout |
| 90 | + ? grepForShortList.stdout.split('\n') |
| 91 | + : []; |
| 92 | + const all = await Promise.all( |
| 93 | + shortList.map(async (path) => { |
| 94 | + const contents = await readFile(path, 'utf8'); |
| 95 | + return { contents, source: './' + path }; |
| 96 | + }), |
| 97 | + ); |
| 98 | + return all.flatMap(({ contents, source }) => |
| 99 | + cleanSplit(contents, ';').map((query) => ({ query, source })), |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +async function findInInlineQueries(root: Directory) { |
| 104 | + const grepForShortList = await $$({ |
| 105 | + reject: false, |
| 106 | + cwd: root.getPath(), |
| 107 | + })`grep -lRE ${` hydrate\\w+ = edgeql`} src --exclude-dir=src/core/edgedb`; |
| 108 | + const shortList = grepForShortList.stdout |
| 109 | + ? root.addSourceFilesAtPaths(grepForShortList.stdout.split('\n')) |
| 110 | + : []; |
| 111 | + return ( |
| 112 | + shortList.flatMap((file) => |
| 113 | + file.getDescendantsOfKind(SyntaxKind.CallExpression).flatMap((call) => { |
| 114 | + if (call.getExpression().getText() !== 'edgeql') { |
| 115 | + return []; |
| 116 | + } |
| 117 | + const args = call.getArguments(); |
| 118 | + |
| 119 | + if ( |
| 120 | + args.length > 1 || |
| 121 | + (!Node.isStringLiteral(args[0]) && |
| 122 | + !Node.isNoSubstitutionTemplateLiteral(args[0])) |
| 123 | + ) { |
| 124 | + return []; |
| 125 | + } |
| 126 | + // Too hard to find parent types that have name |
| 127 | + if (!(call as any).getParent()?.getName()?.includes('hydrate')) { |
| 128 | + return []; |
| 129 | + } |
| 130 | + |
| 131 | + const path = adapter.path.posix.relative( |
| 132 | + root.getPath(), |
| 133 | + call.getSourceFile().getFilePath(), |
| 134 | + ); |
| 135 | + const lineNumber = call.getStartLineNumber(); |
| 136 | + const source = `./${path}:${lineNumber}`; |
| 137 | + |
| 138 | + const query = args[0].getText().slice(1, -1); |
| 139 | + return { query, source }; |
| 140 | + }), |
| 141 | + ) ?? [] |
| 142 | + ); |
| 143 | +} |
| 144 | + |
| 145 | +const RE_SELECT_NAME_AND_FIELDS = /select ([\w:]+) \{((?:.|\n)+)}/i; |
0 commit comments