Skip to content

Commit

Permalink
Fix some bugs in toEdgeQL when using e.with (#1154)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Trinh <scott@scotttrinh.com>
  • Loading branch information
jaclarke and scotttrinh authored Dec 11, 2024
1 parent 90389b0 commit d153164
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions integration-tests/lts/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ describe("update", () => {
})).toEdgeQL();
});

test("nested update and explicit with", async () => {
e.params({ movieId: e.uuid }, (params) => {
const movie = e.select(e.Movie, (m) => ({
filter: e.op(m.id, "=", params.movieId),
}));

const updateChar = e.update(movie.characters, (c) => ({
set: {
name: e.str_lower(c.name),
},
}));

const updateProfile = e.update(movie.profile, (p) => ({
set: {
a: e.str_upper(p.a),
},
}));

return e.with([updateChar, updateProfile], e.select(movie));
}).toEdgeQL();
});

test("scoped update", async () => {
const query = e.update(e.Hero, (hero) => ({
filter_single: e.op(hero.name, "=", data.spidey.name),
Expand Down
9 changes: 9 additions & 0 deletions packages/generate/src/syntax/toEdgeQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ export function $toEdgeQL(this: any) {
// check all references and aliases are within this block
const validScopes = new Set([
withBlock,
// expressions already explictly bound to with block are also valid scopes
...(withBlocks.get(withBlock) ?? []),
...walkExprCtx.seen.get(withBlock)!.childExprs,
]);
for (const scope of [
Expand Down Expand Up @@ -432,6 +434,13 @@ function walkExprTree(
for (const refExpr of expr.__refs__) {
walkExprTree(refExpr, expr.__expr__, ctx);
const seenRef = ctx.seen.get(refExpr as any)!;
if (seenRef.childExprs.includes(expr.__expr__)) {
throw new Error(
`Ref expressions in with() cannot reference the expression to ` +
`which the 'WITH' block is being attached. ` +
`Consider wrapping the expression in a select.`,
);
}
if (seenRef.boundScope) {
throw new Error(`Expression bound to multiple 'WITH' blocks`);
}
Expand Down

0 comments on commit d153164

Please sign in to comment.