From 530e5c686beebe576de2c19b48dfb773d55ba0ca Mon Sep 17 00:00:00 2001 From: Carson Full Date: Wed, 4 Sep 2024 16:29:16 -0500 Subject: [PATCH] Avoid duplicating __typename in generated EdgeQL --- integration-tests/lts/select.test.ts | 17 +++++++++++++++++ packages/generate/src/syntax/toEdgeQL.ts | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/integration-tests/lts/select.test.ts b/integration-tests/lts/select.test.ts index f44c05602..983a82e20 100644 --- a/integration-tests/lts/select.test.ts +++ b/integration-tests/lts/select.test.ts @@ -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, (movie) => ({ + __typename: movie.__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, diff --git a/packages/generate/src/syntax/toEdgeQL.ts b/packages/generate/src/syntax/toEdgeQL.ts index ee4d97a31..696241431 100644 --- a/packages/generate/src/syntax/toEdgeQL.ts +++ b/packages/generate/src/syntax/toEdgeQL.ts @@ -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}`;