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 runable without being wrapped by e.select.

  1. e.select(e.str('Hello world'));
  2. // select 1234;
  3. e.select(e.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. release_year: true,
  5. }));
  6. /*
  7. select Movie {
  8. id,
  9. title,
  10. release_year
  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. release_year: number | undefined;
  6. }[] */

As you can see, the type of release_year is number | 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. release_year: false,
  5. }));
  6. const result = await query.run(client);
  7. // {id: string; title: string | undefined; release_year: never}[]

Selecting all properties

For convenience, the query builder provides a shorthand for selecting all properties of a given object.

  1. e.select(e.Movie, movie => ({
  2. ...e.Movie['*']
  3. }));
  4. const result = await query.run(client);
  5. // {id: string; title: string; release_year: number | null}[]

This * property is just a strongly-typed, plain object:

  1. e.Movie['*'];
  2. // => {id: true, title: true, release_year: true}

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. actors: {
  5. name: true
  6. }
  7. }));
  8. const result = await query.run(client);
  9. /* {
  10. id: string;
  11. title: string;
  12. actors: { 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 EdgeDB, there is minimal danger of conflicting with a property or link named filter. All shapes can contain filter clauses, even nested ones.

  1. e.select(e.Movie, movie => ({
  2. title: true,
  3. actors: actor => ({
  4. name: true,
  5. filter: e.op(actor.name.slice(0, 1), '=', 'A'),
  6. }),
  7. filter: e.op(movie.title, '=', 'Iron Man'),
  8. }));
  1. e.select(e.Movie, movie => ({
  2. title: true,
  3. actors: actor => ({
  4. name: true,
  5. filter: e.op(actor['@character_name'], 'ilike', 'Tony Stark'),
  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. }));

Unlike filter, order_by is not a reserved word in EdgeDB. Using order_by as a link or property name will create a naming conflict and likely cause bugs.

The order_by 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 actors
  10. e.select(e.Movie, movie => ({
  11. order_by: e.count(movie.actors),
  12. }));
  13. /*
  14. select Movie
  15. order by count(.actors)
  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.actors),
  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}[]

Computed fields 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. release_year: e.str("2012"), // TypeError
  4. // you can override links too
  5. actors: e.Person,
  6. }));

Polymorphism

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

  1. select Content {
  2. title,
  3. [is Movie].release_year,
  4. [is TVShow].num_seasons
  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, { release_year: true }),
  4. ...e.is(e.TVShow, { num_seasons: true }),
  5. }));
  6. const result = await query.run(client);
  7. /* {
  8. title: string;
  9. release_year: number | null;
  10. num_seasons: number | null;
  11. }[] */

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

Detached

Sometimes you need to “detach” a set reference from the current scope. (Read the reference docs for details.) You can achieve this in the query builder with the top-level e.detached function.

  1. const query = e.select(e.Person, (outer) => ({
  2. name: true,
  3. castmates: e.select(e.detached(e.Person), (inner) => ({
  4. name: true,
  5. filter: e.op(outer.acted_in, 'in', inner.acted_in)
  6. })),
  7. }));
  8. /*
  9. with outer := Person
  10. select Person {
  11. name,
  12. castmates := (
  13. select detached Person { name }
  14. filter .acted_in in Person.acted_in
  15. )
  16. }
  17. */