Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge default types into modules with the same name (query builder) #793

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions integration-tests/lts/dbschema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,7 @@ module `💯💯💯` {
module extra {
global user_id -> uuid;
}

module User {
scalar type Status extending enum<"Active", "Disabled">;
}
6 changes: 6 additions & 0 deletions integration-tests/lts/dbschema/migrations/00023.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE MIGRATION m1tdrrz5jv7b4z7wo532wow2bdkq2oij5lkatt4vkqxfneise7j32q
ONTO m1rn5yebzwvfjxj5hvugtrug3o7jzhncvdhobjedw24wsni6v3l6ia
{
CREATE MODULE User IF NOT EXISTS;
CREATE SCALAR TYPE User::Status EXTENDING enum<Active, Disabled>;
};
7 changes: 7 additions & 0 deletions integration-tests/lts/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,4 +1368,11 @@ SELECT __scope_0_defaultPerson {
>
>(true);
});

test("Reference default type and module with same name", async () => {
const query = e.select(e.User, () => ({
active: e.User.Status.Active,
}));
await query.run(client);
});
});
16 changes: 12 additions & 4 deletions packages/generate/src/edgeql-js/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ export function generateIndex(params: GeneratorParams) {
`const ExportDefault`,
t`: ${spreadTypes.reverse().join(" & \n ")} & {`,
]);
const defaultSpreadTypes = new Set(
dir.getModule("default").getDefaultExportKeys()
);
index.indented(() => {
for (const [moduleName, internalName] of topLevelModules) {
if (dir.getModule(moduleName).isEmpty()) continue;
index.writeln([
t`${genutil.quote(moduleName)}: typeof _${internalName};`,
]);
let typeStr = `typeof _${internalName}`;
if (defaultSpreadTypes.has(moduleName)) {
typeStr += ` & typeof _default.${moduleName}`;
}
index.writeln([t`${genutil.quote(moduleName)}: ${typeStr};`]);
}
});

Expand All @@ -122,7 +127,10 @@ export function generateIndex(params: GeneratorParams) {
allowFileExt: true,
});

index.writeln([r`${genutil.quote(moduleName)}: _${internalName},`]);
const valueStr = defaultSpreadTypes.has(moduleName)
? `Object.freeze({ ..._${internalName}, ..._default.${moduleName} })`
: `_${internalName}`;
index.writeln([r`${genutil.quote(moduleName)}: ${valueStr},`]);
}
});
index.writeln([r`};`]);
Expand Down