Parameters​

You can pass strongly-typed parameters into your query with e.params.

  1. const helloQuery = e.params({ title: e.str }, params =>
  2. e.op(params.name)
  3. );
  4. /* with name := <str>$name
  5. select name;
  6. */

The first argument is an object defining the parameter names and their corresponding types. The second argument is a closure that returns an expression; use the params argument to construct the rest of your query.

Passing parameter data​

To executing a query with parameters, pass the parameter data as the second argument to .run(); this argument is fully typed!

  1. await helloQuery.run(client, { name: "Harry Styles" })
  2. // => "Yer a wizard, Harry Styles"
  3. await query.run(client, { name: 16 })
  4. // => TypeError: number is not assignable to string

Top-level usage​

Note that the expression being run must be the one declared with e.params; in other words, you can only use e.params at the top level of your query, not as an expression inside a larger query.

  1. const wrappedQuery = e.select(helloQuery);
  2. await e.select(helloQuery).run(client, {name: "Harry Styles"});
  3. // TypeError

Parameter types​

In EdgeQL, parameters can only be primitives or arrays of primitives. That’s not true with the query builder! Parameter types can be arbitrarily complex. Under the hood, the query builder serializes the parameters to JSON and deserializes them on the server.

  1. const complexParams = e.params(
  2. {
  3. title: e.str,
  4. runtime: e.duration,
  5. cast: e.array(
  6. e.tuple({
  7. name: e.str,
  8. character_name: e.str,
  9. })
  10. ),
  11. },
  12. params => e.insert(e.Movie, {
  13. // ...
  14. })
  15. );
  16. await insertMovie.run(client, {
  17. title: "Dune",
  18. runtime: new edgedb.Duration(0, 0, 0, 0, 2, 35),
  19. cast: [
  20. {name: "Timmy", character_name: "Paul"},
  21. {name: "JMo", character_name: "Idaho"},
  22. ]
  23. })