Skip to content

Commit

Permalink
Avoid duplicating __typename in generated EdgeQL
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Sep 4, 2024
1 parent 3be27a5 commit 3c1439c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions integration-tests/lts/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,23 @@ SELECT __scope_0_defaultPerson {
>(true);
});

test("polymorphic with explicit __typename is not duplicated", async () => {
const query = e.select(e.Movie.characters, (person) => ({
__typename: person.__type__.name,
...e.is(e.Villain, { nemesis: true }),
}));

assert.equal(
query.toEdgeQL(),
`WITH
__scope_0_defaultPerson := DETACHED default::Movie.characters
SELECT __scope_0_defaultPerson {
single __typename := __scope_0_defaultPerson.__type__.name,
[IS default::Villain].nemesis
}`,
);
});

test("polymorphic field in nested shape", async () => {
const query = e.select(e.Movie, (movie) => ({
title: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/generate/src/syntax/toEdgeQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ function shapeToEdgeQL(
if (lines.length === 0 && injectImplicitId) {
addLine("id");
}
if (hasPolyEl) {
if (hasPolyEl && !seen.has("__typename")) {
addLine("__typename := .__type__.name");
}
return keysOnly ? `{${lines.join(", ")}}` : `{\n${lines.join(",\n")}\n}`;
Expand Down

0 comments on commit 3c1439c

Please sign in to comment.