Skip to main content

Module define

Module define 

Source
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§

MutatorDefinition
A named mutator (defineMutator): A is its arguments, C the client’s context, and O what its optimistic run returns to the caller’s client outcome (() for nothing). Make one with define_mutator.
QueryDefinition
A named query (defineQuery): A is its arguments, C the client’s context, and Query<R, T> what it builds. Make one with define_query.
QueryRequest
A QueryDefinition applied to its arguments: what Zero::materialize, run, and preload take, which build it with the client’s context. It is Send, so an app can make one on one thread for a client on another.

Traits§

NamedMutator
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.
NamedQuery
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: name in the TS registry, and its body.
define_query
A named query: name in the TS registry, and the query body builds from the arguments and the client’s context.