Skip to content

Commit

Permalink
Fix querybuilder generated query when using unpathed scoped expr
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclarke committed Feb 7, 2025
1 parent efd7ac5 commit d34d047
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
27 changes: 27 additions & 0 deletions integration-tests/lts/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1712,4 +1712,31 @@ SELECT __scope_0_defaultPerson {
const result = await e.select(e.json(-0)).run(client);
assert.equal(result, 0);
});

test("select using scoped expr directly (not a field/path on that scoped expr)", async () => {
const query = e.select(
e.select(e.Movie, (movie) => ({
filter: e.op(movie.title, "=", "Dune"),
})),
(movie) => ({
comp: movie.is(e.Movie),
}),
);
await query.run(client);

const user = e.select(e.User, () => ({
filter_single: { id: "4d0f90b1-de94-4c79-ba56-3e0acdfbd06d" },
}));
const movies = e.select(user.favourite_movies, (movie) => ({
filter: e.op(movie.title, "=", user.username),
}));
const query2 = e.with(
[user, movies],
e.select(movies, (movie) => ({
isFav: e.op(movie, "in", user.favourite_movies),
})),
);

await query2.run(client);
});
});
22 changes: 11 additions & 11 deletions packages/generate/src/syntax/toEdgeQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ function walkExprTree(

const expr = _expr as SomeExpression;

if ((expr as any).__scopedFrom__ != null) {
// If expr is marked as being a scoped copy of another expr, treat it as
// an opaque reference and don't walk it. The enclosing select/update that
// owns the scope will walk the actual unscoped expr.
return [expr];
}

function walkShape(shape: object) {
for (let param of Object.values(shape)) {
if (param.__kind__ === ExpressionKind.PolyShapeElement) {
Expand Down Expand Up @@ -473,16 +480,9 @@ function walkExprTree(
case ExpressionKind.PathLeaf:
case ExpressionKind.PathNode:
if (expr.__parent__) {
if ((expr.__parent__.type as any).__scopedFrom__) {
// if parent is scoped expr then don't walk expr
// since it will already be walked by enclosing select/update

childExprs.push(expr.__parent__.type as any);
} else {
childExprs.push(
...walkExprTree(expr.__parent__.type, parentScope, ctx),
);
}
childExprs.push(
...walkExprTree(expr.__parent__.type, parentScope, ctx),
);

if (
// is link prop
Expand Down Expand Up @@ -1226,7 +1226,7 @@ ${indent(groupStatement.join("\n"), 4)}
);
}
} else if (expr.__kind__ === ExpressionKind.TypeIntersection) {
return `${renderEdgeQL(expr.__expr__, ctx)}[IS ${
return `${renderEdgeQL(expr.__expr__, ctx, false)}[IS ${
expr.__element__.__name__
}]`;
} else if (expr.__kind__ === ExpressionKind.For) {
Expand Down

0 comments on commit d34d047

Please sign in to comment.