With blocks​

During the query rendering step, the number of occurrences of each expression are tracked. All expressions that are referenced more than once and are not explicitly defined in a WITH block (with e.with), are extracted into the nearest WITH block that encloses all usages of the expression.

  1. const a = e.set(e.int64(1), e.int64(2), e.int64(3));
  2. const b = e.alias(a);
  3. e.select(e.plus(a, b)).toEdgeQL();
  4. // WITH
  5. // a := {1, 2, 3},
  6. // b := a
  7. // SELECT a + b

This hold for expressions of arbitrary complexity.

  1. const newActor = e.insert(e.Person, {
  2. name: "Colin Farrell"
  3. });
  4. const newMovie = e.insert(e.Movie, {
  5. title: "The Batman",
  6. cast: newActor
  7. });
  8. const query = e.select(newMovie, ()=>({
  9. id: true,
  10. title: true,
  11. cast: { name: true }
  12. }));

To embed WITH statements inside queries, you can short-circuit this logic with a “dependency list”. It’s an error to pass an expr to multiple e.with``s, and an error to use an expr passed to ``e.with outside of that WITH block in the query.

  1. const newActor = e.insert(e.Person, {
  2. name: "Colin Farrell"
  3. });
  4. e.insert(e.Movie, {
  5. cast: e.with([newActor], // list "dependencies";
  6. e.select(newActor, ()=>({
  7. id: true,
  8. title: true,
  9. }))
  10. )
  11. })