/boxel-skills:query-backed-relationshipsfrom boxel-skills
Use when declaring or reviewing a query-backed `linksTo`/`linksToMany` — the `{ query }` form that resolves by running a search instead of holding authored links. Covers the one thing that surprises everyone (the field holds a bounded page of its query, not the whole match set), reading `totalMatchCount` instead of counting rows, declaring a larger page, `eager: false`, and when the field is the wrong tool. Activates on "count the linked cards", "inverse relationship", "linksToMany with a query", a rollup whose number looks too low or stuck, and "why does my field only have 500 items".
Ships in boxel-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
The { query } form of a relationship, and the bounded page nobody expects.
A linksTo / linksToMany declared with a query resolves by running a search
rather than holding links someone authored:
@field everyActivity = linksToMany(() => Activity, {
query: { filter: { eq: { 'classroom.id': '$this.id' } } },
});
It then reads like any other relationship — this.everyActivity is an array of
Activity instances — and stays current as matching cards are written. This is
the "inverse side" of a link: derived from a filter instead of stored on the
card.
The one thing to internalize: the field holds a bounded page of its query, not the whole match set. Everything below follows from that.
A rollup that reduces over the field is wrong the moment the match count passes the page ceiling — and nothing throws, nothing warns in the card, and the number looks entirely plausible:
// WRONG. Silently counts the page, not the matches.
@field activityCount = contains(NumberField, {
computeVia: function (this: Classroom) {
return this.everyActivity.length;
},
});
A classroom with 600 activity updates reports 500. Read the count off the
field's status instead — it comes from the search's own COUNT(*), which no page
ever bounds:
import { getRelationshipMembershipState } from '@cardstack/base/card-api';
@field activityCount = contains(NumberField, {
computeVia: function (this: Classroom) {
let { totalMatchCount } = getRelationshipMembershipState(
this,
'everyActivity',
);
// Returned as-is. `undefined` means the count is unknown (see below), and
// leaving the field empty says that; `?? 0` would render a confident nought
// over a set nobody counted.
return totalMatchCount;
},
});
The ceiling then stops mattering for this field entirely — no rows are held to
count. Same for "are there any?": (totalMatchCount ?? 0) > 0 reads a genuine
zero and an unknown alike as "none", so where that difference matters, branch on
totalMatchCount === undefined first. Either way it beats
this.everyActivity.length > 0, which is right only by luck once the field is
truncated.
Any other aggregate — a sum, an average, a max over a field of the matches —
genuinely needs the rows, so it needs a page big enough to hold them and
should check isPartial before publishing a number. See
bxl-authoring §7 for aggregating over an inverse
in BXL.
let { membership, isLoading, isLoaded, totalMatchCount, isPartial } =
getRelationshipMembershipState(this, "everyActivity");
| means | |
|---|---|
membership | one RelationshipState per row the field holds (undefined until resolved) |
isLoading | a fetch or search is in flight — bind this to a spinner |
isLoaded | membership is settled |
totalMatchCount | how many instances the query matches, page or no page |
isPartial | true when membership falls short of that count |
membership is per-slot resolution state, not the cards themselves — read
this.everyActivity for those. Each entry says whether its slot resolved, and
to what; the states and how to traverse them safely are in
boxel/references/defensive-link-traversal.md.
Two traps in that table:
isLoaded is not permission to trust a reduction. A truncated set is
settled too. isPartial is the one that licenses it.totalMatchCount absent means unknown, never zero. The field hasn't
resolved, or one of the realms it targets failed and took its share of the
count with it. A genuine empty match reports 0. Don't let ?? 0 collapse
those two into one answer where the difference matters.For the spinner pattern and the observe-only rule (isLoading never starts
the load — the template must render the field too), see
boxel/references/relationship-loading-state.md.
Default: a field declaring no page takes the default ceiling (500). A query
matching more settles holding the first 500, in sort order, with
isPartial: true.
To opt into more, declare the page you need. It is honored up to a hard maximum (2000) everywhere the field resolves:
@field everyActivity = linksToMany(() => Activity, {
query: {
filter: { eq: { 'classroom.id': '$this.id' } },
page: { size: 1500 },
},
});
Above the maximum the page clamps to the maximum and the realm logs that it did,
so you get 2000 rows and isPartial: true rather than an error — a field's page
is read on every index of every instance of that card, so an over-large one must
never make the card unindexable.
Ask for what you need, not for the maximum. The page is a cost paid on every resolution of every instance: the rows are serialized into the owner's document, and each one's own query fields resolve in the next layer of the same pass. A field sized 2000 "just in case" pays for 2000 every time it resolves.
A field that can only ever hold a bounded prefix is a poor fit for "show the user everything matching this". For a displayed list, reach for the search component — it runs on the prerendered-HTML leg, which the page ceiling doesn't apply to, and it pages lazily:
<@context.searchResultsComponent @query={{this.query}} as |results| />
Its results.meta.page.total is the same true count totalMatchCount gives
you. See the count-tile and table patterns in
boxel-patterns.
Rule of thumb: a query-backed field is for a relationship the card reasons over, and for a home or app section that lists a type's cards with delegated render; a search component is for a list that can grow past a few hundred cards, or needs search or filter UI.
eager: falseA query-backed field resolves when its owner card loads, so a rollup over one is current without anything having read the field. Opt out when the query is expensive and rarely read, and it resolves on first access instead:
@field everyActivity = linksToMany(() => Activity, {
query: { filter: { eq: { 'classroom.id': '$this.id' } } },
eager: false,
});
Cost of opting out: until something reads the field it reports
isLoading: false, isLoaded: false, no membership — and it holds no
realm-event subscription, so it won't refresh when matching cards are written.
linksToA linksTo with a query surfaces the query's first match; its page is
forced to size 1 by design. So its sort is load-bearing — "first" is whatever
the sort says:
@field mostRecentActivity = linksTo(() => Activity, {
query: {
filter: { eq: { 'classroom.id': '$this.id' } },
sort: [{ by: 'createdAt', direction: 'desc' }],
},
});
Its status reports a one-element membership, and totalMatchCount is
undefined / isPartial is false — the matches behind it are the field
working as declared, not a shortfall.
$this.<path> reads the owner's own serialized attributes; $REALM
interpolates the owner's realm. Absent an explicit realm / realms, the query
targets the realm holding the instance.
A path that walks through a link — '$this.classroom.id' — resolves on a
loaded card but not in the index, which interpolates from the owner's own
attributes plus id and never its relationships (as of 2026-10). There the
value is undefined, the query is dropped, and the field indexes empty
(links.self: null) with no error, while the same card in the app looks right.
Copy the value into a computed attribute and interpolate that:
@field classroomId = contains(StringField, {
computeVia: function (this: Activity) {
return this.classroom?.id;
},
});
// query: { filter: { eq: { 'classroom.id': '$this.classroomId' } } }
This works only when classroom is a stored link: a computed that reads a
query-backed field is left out of the indexed document, so it is missing at
index time too. Check the index with boxel search, not the app.
bxl-authoring §7 covers the staleness
consequences for aggregates.totalMatchCount goes undefined because the count is no longer
knowable.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
boxel-skills@cardstack-boxel is listed by cardstack/boxel — review the source before installing
npx claudepluginhub cardstack/boxel --plugin boxel-skills27 skills in this plugin
/api-integrationsUse when a card needs data from, or sends data to, a third-party API — "show my GitHub pull requests in a card", "pull my open issues", "a card that calls the weather API with my key", "integrate with Stripe/Twilio/OpenAI", "how often can this card poll", "build a PR inbox". Takes a card author from "I want data from this vendor" to a working card: classifying the integration (no key, a key the workspace shares, the user's own key, a key that can write), choosing a live card or a scheduled pipeline (and why private data takes the pipeline), declaring the `callApi` operations with labels and credential slots, estimating polling cost, and recipes for GitHub (GraphQL, a GitHub App or a read-only fine-grained token, the PR inbox as a pipeline and as a live card), for a plain API key sent in a header, and for images from an AI vendor called with the user's own key (an automation run's own AI requests carry text only). Activates on "third-party API", "vendor API", "integration", "pipeline", "live card", "On a schedule and when the card is opened", "poll", "GitHub API", "GraphQL", `callApi`, `{{userKey}}`, `{{apiKey}}`, "image generation", "AI vendor key", "automation image"./api-keysUse when a card or the AI assistant needs a user's API key, or a key shared into a workspace — "this card needs my GitHub token", "the forecast operation was refused binding-not-approved", "let the user add their key", "share my key with this workspace", "add a key button". Also "sign in to GitHub from this card", "connect my account", "the operation was refused credential-rejected", "the service rejected my key", "should I register a GitHub App or an OAuth App". Covers getting a key from the user only through Boxel's secure window, with the open-secure-window tool: opening it from a card through `@context.toolContext` or from the assistant, its modes (enter a key, replace the key an operation sends, share a key, connect an account through OAuth, approve a card's requests for scheduled pipelines or also for when the card is opened, review what changed in them), passing only the operation the card is blocked on and letting the window gather the card's other operations into one approval, what the tool answers, the blocked-popup fallback, telling the user to check the address bar, what to do when the service rejects a key (stop polling, offer Try again, and Reconnect, which opens the window to replace the key), OAuth clients and connections (when to use a connection rather than a static key, what the card declares, opening connect mode, `credential-rejected` and its reasons, sharing a client into a workspace, and why GitHub takes a GitHub App), and never asking for a credential in the chat or building a form for one. Also covers asking first with `@context.keyReadiness` — the answers (`ready`, `needs-connect`, `needs-approval`, `needs-reapproval`, `needs-admin`, `needs-sign-in`, `rate-limited`, `over-budget`), what a card shows for each and what the secure window can fix, and treating `undefined` as "not known yet" so prerendered HTML never shows Connect. Also covers a call refused for the key owner's limits (`rate-limited`, `over-budget`, `rate-limit-unavailable`): backing off by `retryAfterSeconds` and saying the limit is reached without naming a budget. Activates on "approve", "Allow", "review changes", `needs-approval`, `needs-reapproval`, `open-secure-window`, `OpenSecureWindowTool`, `keyReadiness`, "secure window", "API key", "enter a key", "replace a key", "share a key", "OAuth", "connect", "Reconnect", "GitHub App", `{{userKey}}`, `{{apiKey}}`, `binding-not-approved`, `interactive-use-not-approved`, `anonymous-use-not-approved`, "On a schedule and when the card is opened", "people who aren't signed in", "anonymous cap", `credential-rejected`, `rate-limited`, `over-budget`.