Skip to the page

Feature by feature

Chapter 4 of 11

Rules, queries and files

Access policies live on your schema types rather than inside route handlers. Saved EdgeQL files turn your schema into named endpoints, direct file uploads and typed client SDKs.

Every object type in a freshly pushed schema starts closed over the API. Until an access policy grants permission, queries return zero rows and writes fail, even when a caller sends an unfiltered query.

Try it

Sign up as Ada in the starter app and save a note. Then sign in as Ben in a second browser window and query every Note.

Ben’s query
select Note { title }
An unfiltered query under Ben’s session returns none of Ada’s notes.

Policies on the type#

Pillar at work: Caller-bound

An access policy lives inside an object type and declares which operations (select, insert, update, delete or all) are allowed when its using expression evaluates to true. Written in EdgeQL, policies can read properties, traverse links and check global caller.

dbschema/default.gel
global caller := global ext::auth::ClientTokenIdentity; type Note {  required owner: ext::auth::Identity;  required title: str;  required body: str;   access policy owner_has_full_access    allow all    using (.owner ?= global caller);} type Attachment extending ext::files::File {  required owner: ext::auth::Identity;   access policy owner_has_full_access    allow all    using (.owner ?= global caller);}
The same three-line ownership policy protects both Note rows and Attachment files.

Types without a matching policy stay closed, returning zero records on reads and refusing writes. Napalm verifies the bearer token before binding global caller, so no request can spoof another user’s identity.

Policies in every query

Matching policies compile into the SQL statement while closed types return nothing.

Statement-level enforcement#

Pillar at work: Checked and fault-tested

Napalm compiles matching policies straight into the single SQL statement PostgreSQL runs. Reads skip unpermitted rows, and insert or update statements validate the resulting row before committing, so Ben cannot insert a note naming Ada as .owner.

Service keys and agents pass through the same check. A backend service key belongs to no user, so global caller is empty and workers see zero rows unless you add a policy checking service globals. An OAuth session approved by Ada binds global caller to Ada and restricts the client to her permitted rows.

When debugging permissions, run napalm people reach to count reachable rows per type or napalm explain-access to inspect every policy clause on a specific object ID without exposing field values.

Your terminal
napalm explain-access notes 0192c3f4-6b1a-7d8e-9f00-1a2b3c4d5e6f --as ben@example.com
Object default::Note 0192c3f4-6b1a-7d8e-9f00-1a2b3c4d5e6f
Access × read × change × delete
owner_has_full_access allows all
× .owner ?= global caller
global caller was set
Inspect why a specific Note ID was hidden from Ben without adding debug routes or reading private text.

Saved queries and typed clients#

Pillar at work: Compiled

In production, you store application reads and writes as .edgeql files in dbschema/queries/. Because ownership is already enforced on Note, myNotes.edgeql needs no manual owner filter.

dbschema/queries/myNotes.edgeql
select Note { id, title, body }order by .title
The query omits a manual owner filter because the policy on Note scopes every read.

Saved queries traverse nested links, backlinks and aggregates in one SQL statement with no N+1 round trips. Pushing deploys each file as POST /query/{name}. Callers pass typed JSON variables, including optional filter and cursor parameters, while query text remains on the server so user input cannot alter query structure.

Terminal
curl https://notes.napalmdata.com/query/addNote \  -H "Authorization: Bearer $SESSION" \  -H "Content-Type: application/json" \  -d '{ "variables": { "title": "Printers", "body": "Call the vendor on Monday" } }'
Call addNote over HTTPS with a bearer session and typed JSON variables.

Running napalm generate reads those same deployed queries and generates typed client SDKs in eight languages alongside project CLI commands and MCP tools.

Terminal
napalm schema push notesnpm run generate
Validate and deploy schema and queries, then generate typed client bindings.

Files as typed records#

Pillar at work: One typed graph

Subtyping ext::files::File keeps file metadata and access policies in PostgreSQL while raw bytes live in dedicated object storage. Inserting an Attachment checks the insert policy and returns a one-time upload plan while the record starts in Pending.

dbschema/queries/addFile.edgeql
select (insert Attachment {  owner := global caller,  file_name := <str>$name,  file_size := <int64>$size,}) { id, file_upload }
Inserting a File subtype checks insert policies and returns a direct object-storage upload plan.

Uploads go directly from the browser or phone to your cluster’s bucket without streaming through query engines. Once the transfer completes, Napalm inspects the leading file bytes against any file_format constraint before setting file_status to Ready.

Direct storage uploads

Uploads stream straight to object storage and pass a header check before becoming Ready.

Selecting file_download_url signs a short-lived link inside the policy-filtered query, so unauthorized callers never receive a URL. Uploaded objects are immutable, keeping point-in-time restores and data-cloned branches consistent with historical rows.

dbschema/queries/myFiles.edgeql
select Attachment { id, file_name, file_size, file_status, file_download_url }order by .file_name
Short-lived download URLs are signed only for Attachment rows the caller’s select policy allows.

Checks in CI and production#

Every schema push validates all files in dbschema/queries/ before running DDL. In pull-request CI, napalm schema push --review outputs the migration plan and exits non-zero if a change would break any deployed query.

Terminal
napalm schema push notes --review > review.md
Generate a pull-request migration report and fail CI on broken queries.

Once your client apps use generated functions exclusively, disable ad-hoc EdgeQL execution for user sessions while keeping full query access for backend service keys. Under live traffic, napalm insights notes reports per-query latency, replica read share, policy cost and index recommendations.

Terminal
napalm endpoint notes --passthrough keys
Limit browser and mobile sessions to deployed queries.

Once queries and access policies are deployed, keeping screens synchronized across tabs and devices requires zero extra backend code.

Type a word or two to search every docs page.