Select​

The full power of the EdgeQL select statement is available as a top-level e.select function. All queries on this page assume the Netflix schema described on the Objects page.

Selecting scalars​

Any scalar expression be passed into e.select, though it’s often unnecessary, since expressions are run``able without being wrapped by ``e.select.

  1. e.select(e.str('Hello world'));
  2. // select 1234;
  3. e.select(a.op(e.int64(2), '+', e.int64(2)));
  4. // select 2 + 2;

Selecting free objects​

Select a free object by passing an object into e.select

  1. e.select({
  2. name: e.str("Name"),
  3. number: e.int64(1234),
  4. movies: e.Movie,
  5. });
  6. /* select {
  7. name := "Name",
  8. number := 1234,
  9. movies := Movie
  10. } */

Selecting objects​

As in EdgeQL, selecting an set of objects without a shape will return their id property only. This is reflected in the TypeScript type of the result.

  1. const query = e.select(e.Movie);
  2. // select Movie;
  3. const result = await query.run(client);
  4. // {id:string}[]

Shapes​

To specify a shape, pass a function as the second argument. This function should return an object that specifies which properties to include in the result. This roughly corresponds to a shape in EdgeQL.

  1. const query = e.select(e.Movie, ()=>({
  2. id: true,
  3. title: true,
  4. runtime: true,
  5. }));
  6. /*
  7. select Movie {
  8. id,
  9. title,
  10. runtime
  11. }
  12. */

Note that the type of the query result is properly inferred from the shape. This is true for all queries on this page.

  1. const result = await query.run(client);
  2. /* {
  3. id: string;
  4. title: string;
  5. runtime: Duration | undefined;
  6. }[] */

As you can see, the type of runtime is Duration | undefined since it’s an optional property, whereas id and title are required.

Passing a boolean value (as opposed to a true literal), which will make the property optional. Passing false will exclude that property.

  1. e.select(e.Movie, movie => ({
  2. id: true,
  3. title: Math.random() > 0.5,
  4. runtime: false,
  5. }));
  6. const result = await query.run(client);
  7. // {id: string; title: string | undefined; runtime: never}[]

Nesting shapes

As in EdgeQL, shapes can be nested to fetch deeply related objects.

  1. const query = e.select(e.Movie, () => ({
  2. id: true,
  3. title: true,
  4. cast: {
  5. name: true
  6. }
  7. }));
  8. const result = await query.run(client);
  9. /* {
  10. id: string;
  11. title: string;
  12. cast: { name: string }[]
  13. }[] */

Why closures?​

In EdgeQL, a select statement introduces a new scope; within the clauses of a select statement, you can refer to fields of the elements being selected using leading dot notation.

  1. select Movie { id, title }
  2. filter .title = "The Avengers";

Here, .title is shorthand for the title property of the selected Movie elements. All properties/links on the Movie type can be referenced using this shorthand anywhere in the select expression. In other words, the select expression is scoped to the Movie type.

To represent this scoping in the query builder, we use function scoping. This is a powerful pattern that makes it painless to represent filters, ordering, computed fields, and other expressions. Let’s see it in action.

Filtering​

To add a filtering clause, just include a filter key in the returned params object. This should correspond to a boolean expression.

  1. e.select(e.Movie, movie => ({
  2. id: true,
  3. title: true,
  4. filter: e.op(movie.title, 'ilike', "The Matrix%")
  5. }));
  6. /*
  7. select Movie {
  8. id,
  9. title
  10. } filter .title ilike "The Matrix%"
  11. */

Since filter is a reserved keyword in EdgeQL, there is minimal danger of conflicting with a property or link named filter. All shapes can contain filter clauses, even nested ones.

### Nested filtering

  1. e.select(e.Movie, movie => ({
  2. title: true,
  3. cast: actor => ({
  4. name: true,
  5. filter: e.op(actor.name.slice(0, 1), '=', 'A'),
  6. }),
  7. filter: e.op(movie.title, '=', 'Iron Man'),
  8. }));

Ordering​

As with filter, you can pass a value with the special order_by key. To simply order by a property:

  1. e.select(e.Movie, movie => ({
  2. order_by: movie.title,
  3. }));

The order key can correspond to an arbitrary expression.

  1. // order by length of title
  2. e.select(e.Movie, movie => ({
  3. order_by: e.len(movie.title),
  4. }));
  5. /*
  6. select Movie
  7. order by len(.title)
  8. */
  9. // order by number of cast members
  10. e.select(e.Movie, movie => ({
  11. order_by: e.count(movie.cast),
  12. }));
  13. /*
  14. select Movie
  15. order by count(.cast)
  16. */

You can customize the sort direction and empty-handling behavior by passing an object into order_by.

  1. e.select(e.Movie, movie => ({
  2. order_by: {
  3. expression: movie.title,
  4. direction: e.DESC,
  5. empty: e.EMPTY_FIRST,
  6. },
  7. }));
  8. /*
  9. select Movie
  10. order by .title desc empty first
  11. */

Order direction

e.DESC e.ASC

Empty handling

e.EMPTY_FIRST e.EMPTY_LAST

Pass an array of objects to do multiple ordering.

  1. e.select(e.Movie, movie => ({
  2. title: true,
  3. order_by: [
  4. {
  5. expression: movie.title,
  6. direction: e.DESC,
  7. },
  8. {
  9. expression: e.count(movie.cast),
  10. direction: e.ASC,
  11. empty: e.EMPTY_LAST,
  12. },
  13. ],
  14. }));

Pagination​

Use offset and limit to paginate queries. You can pass an expression with an integer type or a plain JS number.

  1. e.select(e.Movie, movie => ({
  2. offset: 50,
  3. limit: e.int64(10),
  4. }));
  5. /*
  6. select Movie
  7. offset 50
  8. limit 10
  9. */

Computeds​

To add a computed field, just add it to the returned shape alongside the other elements. All reflected functions are typesafe, so the output type

  1. const query = e.select(e.Movie, movie => ({
  2. title: true,
  3. uppercase_title: e.str_upper(movie.title),
  4. title_length: e.len(movie.title),
  5. }));
  6. const result = await query.run(client);
  7. /* =>
  8. [
  9. {
  10. title:"Iron Man",
  11. uppercase_title: "IRON MAN",
  12. title_length: 8
  13. },
  14. ...
  15. ]
  16. */
  17. // {name: string; uppercase_title: string, title_length: number}[]

Computables can “override” an actual link/property as long as the type signatures agree.

  1. e.select(e.Movie, movie => ({
  2. title: e.str_upper(movie.title), // this works
  3. runtime: e.int64(55), // TypeError
  4. // you can override links too
  5. cast: e.Person,
  6. }));

Polymorphism​

EdgeQL supports polymorphic queries using the [is type] prefix.

  1. select Content {
  2. title,
  3. [is Movie].runtime,
  4. [is TVShow].num_episodes
  5. }

In the query builder, this is represented with the e.is function.

  1. e.select(e.Content, content => ({
  2. title: true,
  3. ...e.is(e.Movie, { runtime: true }),
  4. ...e.is(e.TVShow, { num_episodes: true }),
  5. }));
  6. const result = await query.run(client);
  7. /* {
  8. title: string;
  9. runtime: Duration | null;
  10. num_episodes: number | null;
  11. }[] */

The runtime and num_episodes properties are nullable to reflect the fact that they will only occur in certain objects.

Detached​

  1. const query = e.select(e.Person, (outer) => ({
  2. name: true,
  3. castmates: e.select(detachedPerson, (inner) => ({
  4. name: true,
  5. filter: e.op(
  6. e.op(outer.acted_in, 'in', inner.acted_in),
  7. 'and',
  8. e.op(outer, '!=', inner) // don't include self
  9. ),
  10. })),
  11. }));