Cratis Screenplay queries: read application state
A Cratis Screenplay query reads application state without changing it. Use one when a screen or caller needs a read model, either as a single instance or a collection written with [].
Choose the result shape first. Add by to identify an instance or filter to narrow a collection, and declare the policies that authorize the read. A query answers once unless its return type is observable, which requests updates as the view changes. That declaration is a contract for the realizing host, not proof that every execution tool can deliver live updates.
For a query over projected state, start without a performer. Attach one when the result needs code or an external read; the reference evaluator does not execute that attachment. Execution tools consume a normalized executable semantic model (ESM), whose limits are described below. Once the read is declared, specify its result to check values and missing-instance behavior.
Syntax
Section titled “Syntax”query <Name> => [observable] <ReturnType>[[]] [optional] [description "<text>"] [by <paramName> <Type> [from <source>]] [filter <paramName> <Type> [optional] [from <source>]] [scoped to <scope>] [authorize <PolicyName> [or <PolicyName>]*] [performer file <Path> | ```csharp <C# returning the result> ``` | ```sql <SQL returning the result> ```]| Clause | Meaning |
|---|---|
observable |
The query is a live read — it keeps pushing as the read model changes, instead of answering once. |
description |
What the query is trying to accomplish, in prose. |
by |
The identifying parameter — the query returns the instance it identifies. |
filter |
A caller-supplied equality parameter narrowing the result set. Write optional after the type when the caller may omit the filter; otherwise it is required. |
from |
Fills the parameter from the query context instead of the caller. |
authorize |
The policies that must pass. |
performer |
The code that performs the query — an external file, or an inline block. |
Description
Section titled “Description”A query’s description says what the read is for, in the words of the domain. It is the first body line, at most one per query, and takes either the quoted single-line form or a fenced block — the same shape as a command description:
query GetOverdueInvoices => OverdueInvoicesReadModel[] description "Invoices past their due date, oldest first, for the collections worklist" authorize IsAccountantThat sentence is what a reader — human or LLM — has to work from when generating or reviewing an implementation, so write the intent, not a restatement of the name.
Observable queries
Section titled “Observable queries”Some reads answer once — a report a user asked for, a lookup a screen does when it opens. Others are meant to stay current: a worklist that gains a row the moment an invoice goes overdue, a status a second user changes while the first is looking at it. Those are two different promises to the caller, and without saying which one a query makes, a document leaves the reader — and the implementation — to guess.
observable qualifies the return type and settles it:
query LiveOverdueInvoices => observable OverdueInvoicesReadModel[] description "Invoices past their due date, kept current on the collections board" authorize IsAccountantRead it as the query yields an observable list of overdue invoices. Without the marker, the query is one-shot — the default, and what most reads are:
query GetInvoice => InvoiceDetailsReadModel description "One invoice with its lines, customer and shipping status" by invoiceId InvoiceIdThe marker qualifies only how the result arrives: observable composes with [] and optional (observable InvoiceDetailsReadModel optional), with by and filter parameters, and with authorize. A screen binds to a live query exactly the way it binds to a one-shot one — data <ReadModel> via query <QueryName>. For observable with a performer, Stage admits the live collection shape over a projected read model through the observable performer contract. Live performers returning a single or optional read model remain unsupported.
The result => observable optional means an observable result whose type is named optional, not an optional result of a type named observable. The leading observable is read first. To make that result optional, write => observable optional optional.
A one-shot query returning an optional scalar type named observable must keep query Q => observable?. This is the one place the ? spelling remains canonical: replacing it with optional changes the meaning. It produces no PLAY0479, and quick fixes and document migrations leave it unchanged. Collections (=> observable[] optional) and live queries (=> observable observable optional) still use the keyword.
What the caller sees
Section titled “What the caller sees”authorize says who may call a query. In the executable semantic model, declarative gates on optional keyed lookups, unkeyed list queries and keyed list queries run before the lookup. A keyed query compares a supplied caller against the by argument; an unkeyed list query has no query-key artifact. A missing caller or failed gate yields Unauthorized; module and feature gates also apply by AND. If evaluation reaches an opaque ESM v3 policy predicate, the reference evaluator returns SemanticUnsupported instead of guessing a decision. Caller-supplied equality filters on collection queries are admitted by ESM v14. Policies still see only the by argument, not filters. A policy that names a query filter reports PLAY0273; declare that argument with by instead to authorize by it. Explicit scopes and context-sourced filters remain authoring syntax and are not admitted into the reference evaluator. A performer-backed query is an opaque read (see below). It says nothing about what they get back — and in a real application those are different questions. All and Mine may admit exactly the same callers and return entirely different rows, and that difference is the access model a reader needs.
scoped to states it:
query Mine => Timesheet[] scoped to identity authorize Consultant
query Everyones => Timesheet[] scoped to global authorize AdministratorWithout it, a query returning only the caller’s own rows reads as one returning everyone’s — the opposite of what it does.
The tenant is the default, and stays unstated
Section titled “The tenant is the default, and stays unstated”A query is scoped to the tenant it runs for unless it says otherwise. That is the overwhelmingly common case, so writing it down would be noise on almost every query in a document:
query ForTenant => Timesheet[]That query is already scoped to the current tenant. Nothing states it, because nothing needs to.
scoped to global is how a query opts out and reaches past the tenant. Making the narrow case the default means the dangerous case is the one you have to write down, rather than the one you get by forgetting.
The scope is a name, not a closed set
Section titled “The scope is a name, not a closed set”identity and global are the two the language documents, but the grammar accepts any name. What scopes exist follows the identity model of whatever runs the document — Screenplay states that a query is scoped and to what, and leaves enforcing it to the runtime. A query declares at most one scope; results are narrowed one way.
Invalid arguments are rejected
Section titled “Invalid arguments are rejected”A malformed lookup is not a missing instance. After authorization and argument shape checks, the reference evaluator validates the by key and every supplied filter value against the concept’s rules, just as it validates command inputs. A rule violation returns Validation, carrying the first failure’s message (including $strings keys) and every validation failure. Failure order is the key first, supplied filters in declaration order, and each concept’s rules in declaration order. This applies in every ESM version without changing model bytes.
An omitted optional filter is not validated. A supplied value with the wrong shape, including a null filter, still rejects as Contract. Authorization denial remains Unauthorized and happens first. An opaque concept rule returns SemanticUnsupported with the Query capability rather than inventing a result. A valid argument matching no instance still returns no result.
From ESM v18, use one actionless query and then error to specify a Validation rejection; do not use then no result for a malformed concept value.
Composite lookups
Section titled “Composite lookups”When a read model’s identity has several parts, supply those parts together rather than joining them into an untyped string.
A bare by header declares at least two name Type [from source] children. It cannot combine with the single by name Type form. Names are read-model key property names. From ESM v27, an optional single-instance query supplies every flat key part with its required compatible nominal type and caller-supplied value. Collection queries use equality filters or one scalar by name Type line, not a by block. See the complete example in Read-model keys.
Collection by blocks, context-sourced parts and non-optional single-instance returns remain refused with PLAY0268. Missing parts use PLAY0661; invalid shapes and parts use PLAY0662. Queries have no key-completion repair.
Parameters
Section titled “Parameters”Parameter sources describe where values should come from. The ESM supports a narrower set than the syntax; check the equality-filter limits below before relying on a contextual source.
by names the identifying parameter; filter narrows the result set. Both are supplied by the caller — a screen, an API client — unless they declare a source with from:
query ListInvoices => InvoiceListReadModel[] description "Every invoice the caller may see, narrowed by status and customer" filter status InvoiceStatus optional filter customerId CustomerId optional filter tenantId TenantId from $context.tenant authorize IsAuthenticatedstatus and customerId come from the UI. The declaration assigns tenantId to the query context, not the caller. The syntax accepts mapping sources, including $env. and constants, but the executable equality-filter contract below refuses filters with from; this example records intent rather than an executable reference query.
Executable equality filters
Section titled “Executable equality filters”In ESM v14 each supplied filter matches the read-model property with the same name using value equality. The property must be a scalar primitive, concept or enum, and the filter must have the same nominal type, ignoring optionality. Names must be distinct and cannot duplicate by. Filters on single-result queries and filters with a from source are refused with PLAY0268, naming the query, filter and reason.
by and every supplied filter combine with AND. An instance missing the filtered property does not match. A required filter must be supplied; an optional filter may be omitted and then does not narrow. Supplied null is refused, even for an optional filter. T optional is the canonical spelling; T? is legacy.
Query specifications name by and filters in their arguments. Unknown, repeated or missing required arguments report PLAY0468. Collection rows compare without regard to order, matched by read model and instance key, with exact row count. See Query results.
Examples
Section titled “Examples”A single-instance query identified by a parameter:
query GetInvoice => InvoiceDetailsReadModel description "One invoice with its lines, customer and shipping status" by invoiceId InvoiceId authorize IsAuthenticatedAn observable list query for a whole screen:
query AllWorkItems => observable WorkItemSummary[] description "Every work item on the board, kept current as work changes"An observable list narrowed by a caller-supplied key:
query CommentsForWorkItem => observable CommentView[] description "All comments for the selected work item, kept live while the detail screen is open" by workItemId WorkItemIdA collection query with optional filters:
query ListInvoices => InvoiceListReadModel[] description "Every invoice the caller may see, narrowed by status and customer" filter status InvoiceStatus optional filter customerId CustomerId optional authorize IsAuthenticatedThe executable semantic model admits unfiltered lists, lists narrowed by one caller-supplied by key, and caller-supplied equality filters on collections, including their observable forms. A model with filters selects ESM v14; models without filters keep their existing bytes, version and revision. scoped to, by ... from <source> and filter ... from <source> remain unadmitted (PLAY0268), each with its own message. A performer query is an opaque read, but context-sourced by and filter arguments on it are refused with PLAY0810. The reference evaluator answers a live query with a snapshot; live change sets, paging and ordering are not part of v14.
Executable query shape
Section titled “Executable query shape”The executable semantic model serializes the admitted collection shapes explicitly so Stage can generate the same query, projection and handler routes in every host:
{ "name": "AllWorkItems", "readModel": "<WorkItemSummary semantic id>", "cardinality": "many", "delivery": "live"}An unkeyed list query has no argument and no keyProperty. It routes to the whole read-model collection.
{ "name": "CommentsForWorkItem", "readModel": "<CommentView semantic id>", "argument": { "name": "workItemId", "type": { "kind": "concept", "concept": "<WorkItemId semantic id>" } }, "keyProperty": "<CommentView.workItemId semantic id>", "cardinality": "many", "delivery": "live"}A keyed list query routes through its caller-supplied argument and matches that value against keyProperty on the read model. Stage should map delivery: "live" to the native observable query lifecycle. delivery: "snapshot" uses the same shape without live updates.
A v14 filtered query writes its filters after the argument/keyProperty group, or after readModel when unkeyed, and before cardinality. This excerpt shows a required status filter and an optional customer filter:
"filters": [ { "id": "<status argument id>", "name": "status", "type": { "kind": "concept", "primitive": null, "target": "<InvoiceStatus id>", "collection": false, "optional": false }, "property": "<status property id>" }, { "id": "<customer argument id>", "name": "customerId", "type": { "kind": "concept", "primitive": null, "target": "<CustomerId id>", "collection": false, "optional": true }, "property": "<customerId property id>" }]There is no separate optional flag on the filter: its type reference carries optionality. Query requests, returned results and specification query results carry only supplied values, after key, in query filter declaration order:
"filters": [ { "filter": "<status argument id>", "value": { "kind": "string", "value": "issued" } }]A query call is identified by the query, its by key and its supplied filter values. Two calls of one unkeyed query with different filters are distinct. Both arrays are omitted when empty. Consumers must explicitly admit v14 or refuse the model rather than drop its filters.
Queries that read a graph
Section titled “Queries that read a graph”A query that ends in graph <Graph> follows a path over a declared graph and returns its read model, flat or with nested rows it collects along an edge. Its match, where and return are described in reading a graph.
The performer block
Section titled “The performer block”Most queries need nothing more than their return type — the read model a projection already builds is the answer. When a snapshot query needs its own logic, performer is where it goes. Stage loads the read-model collection once as readModels and runs a verified synchronous C# body to return the result. It is the query’s counterpart to a command’s handler, and it takes the same two shapes: a file reference, or an inline block.
An observable collection query with a performer has its own binding, described under Observable performers. An observable query that returns one read model, or an optional one, is still an explicit gap in Stage (STAGE-IMPL-006, a method throwing NotImplementedException, outcome Partial).
Delegating to a file:
query GetInvoiceSummary => InvoiceSummaryReadModel description "The counters behind the dashboard header" performer file Queries/InvoiceSummaryPerformer.csInline, in a top-level language:
query LargestPayments => PaymentSummary[] description "The five largest payments" performer ```csharp return readModels.OrderByDescending(payment => payment.Amount).Take(5); ```Or in SQL, when the read is a query against a relational store:
query ListLineItems => InvoiceLineReportReadModel[] description "Every invoice line, priced, for the accounting line report" filter tenantId TenantId from $context.tenant performer ```sql select InvoiceId, LineNumber, Quantity, UnitPrice from InvoiceLineReport where TenantId = @tenantId order by InvoiceId, LineNumber ```Inside a Stage C# performer, context supplies Arguments (PascalCase argument properties), Tenant (string), Identity (ClaimsPrincipal), and ReceivedAt (DateTimeOffset). Caller arguments are also available by their camelCase parameter names. There is no command causation, CausedBy, or event Occurred member. A verified C# file body uses the same bindings; other languages, missing or invalid attachments, and body compilation errors render as gaps rather than executable performers.
Observable performers
Section titled “Observable performers”The binding is defined by decision 0120. It applies to an observable query that returns a collection (=> observable Talk[]); a live query that returns one read model or an optional one has no live contract and stays a host gap.
- Inputs.
readModelsis aLiveReadModels<T>for the declared read modelT,contextis the same performer context as above, and the caller arguments are available by their camelCase names. - Result. The body returns
IObservable<IEnumerable<T>>. Each emission is the complete answer, never a delta, and nothing guarantees that consecutive emissions differ. - Live source.
LiveReadModels<T>is anIObservable<IEnumerable<T>>with no other members. On subscribe it emits the current collection, then the whole collection after each change. The host declares the type; no package does. - Reads. The body reads only the declared read model, through
readModels. A host that cannot observe that read model reports a gap; the binder does not refuse it. - Lifetime. The host subscribes once per client subscription and disposes it on unsubscribe, disconnect or shutdown. Disposal is the only cancellation:
contexthas no cancellation member.OnErrorandOnCompletedend the subscription, and the host does not resubscribe. - Capture.
contextis captured once per subscription, soReceivedAtdoes not advance. - Version. The
QueryPerformerrequirement carriesContextVersion1 andResultVersion2. Every other performer stays at 1 and 1.
public interface LiveReadModels<T> : IObservable<IEnumerable<T>>{}Inline:
query OpenTalks => observable Talk[] filter track String performer ```csharp return readModels.Select(talks => talks.Where(talk => talk.Track == track).OrderBy(talk => talk.Title)); ```Delegating to a file:
query OpenTalks => observable Talk[] filter track String performer file Queries/OpenTalks.csThe content of Queries/OpenTalks.cs is the same method body:
return readModels.Select(talks => talks.Where(talk => talk.Track == track).OrderBy(talk => talk.Title));A query a performer answers is an opaque read
Section titled “A query a performer answers is an opaque read”The provider seam defines PerformAsync for Snapshot Many performers at context and result version 1 (decision 0121); the reference run does not call it while performer queries stay outside the executable model.
The code decides what a performer returns, so no declarative shape can be executed or specified. Binding does not refuse the application merely for having a performer. The query is listed as code the model needs (QueryPerformer), reported as PLAY0270 information (Query '<name>' is performed by code), and left out of the executable model: it takes no part in the plan and no specification can assert it. A specification whose then query names a performer query is left out of the executable specifications with the same information; every other declaration and specification of the application keeps running with screenplay test. Everything else about the query — its read model, the screen bound to it, its authorization — is unchanged.
The QueryPerformer requirement carries the query’s shape, so a host can render the query without running it: the read model it returns (name, semantic identity and address), its cardinality (One, ZeroOrOne or Many, as for any other query), whether it is observable (Live) or a snapshot, the requirement’s ResultVersion (2 for an observable collection, otherwise 1), and its caller-supplied arguments — each by key and key part, then each filter, with its type and whether it is optional. A performer query with a by or filter argument sourced from $context is refused with PLAY0810, naming the query and argument. Context-sourced performer arguments are not supported; use caller-supplied arguments instead. Other arguments sourced with from, such as literals, remain omitted from the shape. The shape lives only on the requirement: it is not part of the executable model, so it changes neither the canonical model bytes nor its version.
A read model a performer composes therefore sits next to projected read models without making the application unbound. Specify the declarative part (the events that feed it) as usual; the performer’s own logic is realization metadata that a test of the real code covers.
A query is complete without a performer. The performer is realization metadata, not a precondition — see Declarative first.
Guidance
Section titled “Guidance”- Name queries as descriptive reads:
GetInvoice,ListInvoices,GetOverdueInvoices. - Mark a read
observablewhen the caller is meant to see changes without asking again; leave it off for a one-shot read. - The return type is a read model built by a projection in the same or another
StateViewslice. - Anything the caller must not be able to choose — the tenant, the caller’s own subject — belongs on a
fromparameter, not on afilterthe UI supplies. - Screens bind to queries with
data <ReadModel> via query <QueryName>.