Expand description
Typed named queries and mutators, behind the serde feature:
define_query and define_mutator (defineQuery in
packages/zql/src/query/query-registry.ts and defineMutator in
packages/zql/src/mutate/mutator.ts, shaped like the Swift client’s).
Each is defined once, by its name in the TS registry the server runs and
a serde argument type, and called with a value of that type: the client
sends the name and the arguments as JSON, as JS does, and reads them back
as the type before a mutator’s body runs. The context comes from the
Zero the request goes to.
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct ProjectArgs { project_name: String }
/// The logged-in user, which `Zero::new` was given.
type Context = Option<String>;
const LABELS: QueryDefinition<ProjectArgs, Context, Label> =
define_query("labels", |args, _ctx| labels_query(&args.project_name));
const RENAME: MutatorDefinition<ProjectArgs, Context> =
define_mutator("project.rename", |tx, args, ctx| Ok(()));
// zero.materialize(&LABELS.with(ProjectArgs { project_name: "Zero".into() }), &ttl)?;
// zero.mutate_request(RENAME.with(ProjectArgs { project_name: "Zero".into() }));
// Zero::new(schema, context, vec![RENAME.mutator()], ...);A definition that takes no arguments takes () (or another type with
no fields), which sends none, as JS’s undefined does.
Structs§
- Mutator
Definition - A named mutator (
defineMutator):Ais its arguments,Cthe client’s context, andOwhat its optimistic run returns to the caller’sclientoutcome (()for nothing). Make one withdefine_mutator. - Query
Definition - A named query (
defineQuery):Ais its arguments,Cthe client’s context, andQuery<R, T>what it builds. Make one withdefine_query. - Query
Request - A
QueryDefinitionapplied to its arguments: whatZero::materialize,run, andpreloadtake, which build it with the client’s context. It isSend, so an app can make one on one thread for a client on another.
Traits§
- Named
Mutator - A named mutator whatever its types: for a list of an app’s mutators, to register them all and to check them against the TS definitions.
- Named
Query - A named query whose arguments come as JSON, as the server gets them, whatever its types: for a list of an app’s queries, such as a test that checks them against the TS definitions.
Functions§
- define_
mutator - A named mutator:
namein the TS registry, and itsbody. - define_
query - A named query:
namein the TS registry, and the querybodybuilds from the arguments and the client’s context.