|
1 | 1 | import { generateQueryBuilder as runQueryBuilderGenerator } from '@edgedb/generate/dist/edgeql-js.js';
|
2 |
| -import { groupBy } from '@seedcompany/common'; |
| 2 | +import { groupBy, many, Many } from '@seedcompany/common'; |
3 | 3 | import type { ts } from '@ts-morph/common';
|
4 | 4 | import { Directory, Node } from 'ts-morph';
|
5 | 5 | import { codecs } from '../codecs';
|
@@ -30,6 +30,13 @@ export async function generateQueryBuilder({
|
30 | 30 | changeImplicitIDType(qbDir);
|
31 | 31 | adjustToImmutableTypes(qbDir);
|
32 | 32 | addTypeNarrowingToStdScalars(qbDir);
|
| 33 | + fixAncestorOverloads(qbDir, { |
| 34 | + 'default::LanguageEngagement': 'project', |
| 35 | + 'default::InternshipEngagement': 'project', |
| 36 | + 'default::ProgressReport': ['container', 'engagement'], |
| 37 | + 'ProgressReport::CommunityStory': 'container', |
| 38 | + 'ProgressReport::Highlight': 'container', |
| 39 | + }); |
33 | 40 | }
|
34 | 41 |
|
35 | 42 | function addJsExtensionDeepPathsOfEdgedbLibrary(qbDir: Directory) {
|
@@ -142,6 +149,32 @@ function addTypeNarrowingToStdScalars(qbDir: Directory) {
|
142 | 149 | });
|
143 | 150 | }
|
144 | 151 |
|
| 152 | +/** |
| 153 | + * Fixes shapes of types that have overloaded a pointer from an ancestor/grandparent. |
| 154 | + * Currently, the QB only works with overloaded pointers of a direct parent. |
| 155 | + */ |
| 156 | +function fixAncestorOverloads( |
| 157 | + qbDir: Directory, |
| 158 | + fqnTypePointerMap: Record<string, Many<string>>, |
| 159 | +) { |
| 160 | + for (const [fqn, pointers] of Object.entries(fqnTypePointerMap)) { |
| 161 | + const module = qbDir.addSourceFileAtPath( |
| 162 | + `modules/${fqn.split('::').slice(0, -1).join('/')}.ts`, |
| 163 | + ); |
| 164 | + const type = fqn.split('::').pop()!; |
| 165 | + const pointerStr = many(pointers) |
| 166 | + .map((p) => `"${p}"`) |
| 167 | + .join(' | '); |
| 168 | + const shape = module.getTypeAliasOrThrow(`$${type}λShape`); |
| 169 | + replaceText(shape, (prev) => |
| 170 | + prev.replaceAll( |
| 171 | + /(?<==.+)[\w._$]+λShape/g, |
| 172 | + (parent) => `Omit<${parent}, ${pointerStr}>`, |
| 173 | + ), |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
| 177 | + |
145 | 178 | const replaceText = <N extends ts.Node>(
|
146 | 179 | node: Node<N>,
|
147 | 180 | replacer: (prevText: string) => string,
|
|
0 commit comments