Skip to content

feat: add @computed directive and custom resolvers - #390

Merged
Sekhmet merged 10 commits into
masterfrom
feat/computed-resolvers
Jun 29, 2026
Merged

Sekhmet merged 10 commits into
masterfrom
feat/computed-resolvers

Conversation

@bonustrack

@bonustrack bonustrack commented May 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds a @computed directive for schema fields resolved at query time via custom SQL subqueries instead of database columns.

  • Resolvers are defined with a single sql function returning a Knex subquery
  • @computed fields are skipped from DB table creation and codegen
  • @computed fields support the full set of orderBy and where operators based on the underlying type (numeric comparisons, string contains, in/not_in, etc.)
  • The SQL subquery is added to SELECT in both list (queryMulti) and single-entity (DataLoader) queries, so the value is available everywhere via standard GraphQL field resolution

Usage

Schema:

type Space {
  id: String!
  name: String!
  active_proposal_count: Int! @computed
}

Checkpoint init:

const checkpoint = new Checkpoint(schema, {
  resolvers: {
    Space: {
      active_proposal_count: {
        sql: (knex) =>
          knex('proposals')
            .count('*')
            .where('proposals.space', knex.ref('spaces.id'))
            .where('proposals._indexer', knex.ref('spaces._indexer'))
            .where('proposals.start', '<=', knex.raw("extract(epoch from now())::integer"))
            .where('proposals.max_end', '>', knex.raw("extract(epoch from now())::integer"))
            .where('proposals.cancelled', false)
            .where('proposals.executed', false)
            .where('proposals.vetoed', false)
            .whereRaw('upper_inf(proposals.block_range)')
      }
    }
  }
});

Queries:

# Sort by active proposals
{ spaces(orderBy: active_proposal_count, orderDirection: desc, first: 10) { id active_proposal_count } }

# Filter spaces with active proposals
{ spaces(where: { active_proposal_count_gt: 0 }, first: 10) { id active_proposal_count } }

# Single-entity query (DataLoader includes the subselect too)
{ space(indexer: "sep", id: "0xabc") { id active_proposal_count } }

Test plan

  • @computed fields excluded from generated DB tables
  • @computed fields excluded from codegen models
  • @computed fields included in orderBy enum and where filters (full type-appropriate set)
  • SQL subquery added to SELECT in list queries (queryMulti)
  • SQL subquery added to SELECT in single-entity / nested queries (DataLoader)
  • ORDER BY works on computed fields (no table prefix)
  • WHERE filters work on computed fields via subquery
  • Non-computed fields continue to work as before
  • Tested locally with sx-monorepo active_proposal_count (list, single, ordered, filtered)

🤖 Generated with Claude Code

Fields marked with @computed are resolved at query time via user-provided
resolver functions instead of being backed by a database column.

- Add @computed directive (skipped from DB table creation, codegen,
  where filters, and orderBy)
- Add `resolvers` option to CheckpointOptions for registering custom
  field resolvers per entity type
- Merge custom resolvers into the auto-generated GraphQL schema

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
bonustrack and others added 3 commits May 27, 2026 16:11
Computed resolvers can now provide a `sql` function alongside `resolve`.
When present, the subquery is added to SELECT and used for ORDER BY
without the table prefix, enabling database-level sorting and pagination
on computed fields.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Drop the function-only shorthand. Always require { sql } config.
resolve is optional and auto-falls back to the sql subselect result
from list queries or 0 for single-entity queries.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Computed Int fields now support the full set of where operators:
equals, _not, _gt, _gte, _lt, _lte, _in, _not_in.

Also simplifies the where filter generation and resolver handling.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copilot AI requested a review from Sekhmet June 24, 2026 14:48
Comment thread src/graphql/controller.ts Resolved
Comment thread src/graphql/controller.ts Resolved
bonustrack and others added 2 commits June 26, 2026 04:04
Address PR review:
- Drop the dual resolve/sql config; sql is the only way to define a
  computed field. DataLoader now adds the same subselect, so single-
  entity and nested queries get the value via standard GraphQL field
  resolution from the parent row (no separate resolver needed).
- Drop the Int-only restriction on where filters. Computed fields now
  fall through the normal field processing and receive the full set of
  type-appropriate filters (Int gt/lt/in/..., String contains/..., etc.)
  with the same suffixes supported in handleWhere via subquery.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- Collapse getDerivedFromDirective/getComputedDirective into a
  single parameterized getDirective helper
- Extract directiveDef/nonNullStringArg factories in extendSchema
- Merge LIKE/ILIKE operators into a map alongside the comparison ops
- Extract getComputedConfigs helper shared by queryMulti and
  the DataLoader (case-insensitive type-name lookup)

No behavior change.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Comment thread src/graphql/resolvers.ts Resolved
Comment thread src/utils/graphql.ts Resolved
Comment thread src/graphql/resolvers.ts Resolved
@Sekhmet
Sekhmet enabled auto-merge (squash) June 29, 2026 15:34

@Sekhmet Sekhmet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@Sekhmet
Sekhmet merged commit 0089a3a into master Jun 29, 2026
1 check passed
@Sekhmet
Sekhmet deleted the feat/computed-resolvers branch June 29, 2026 15:35
tony8713 pushed a commit to snapshot-labs/sx-monorepo that referenced this pull request Jun 29, 2026
Add active_proposal_count as a @computed field on Space in the API,
resolved at query time by counting proposals where start <= now and
max_end > now. Display the active count in the explore page for all
protocols and sort onchain spaces by active proposals.

Depends on snapshot-labs/checkpoint#390

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sekhmet pushed a commit to snapshot-labs/sx-monorepo that referenced this pull request Jun 29, 2026
* feat: show active proposals count for Snapshot X and Governor spaces

Add active_proposal_count as a @computed field on Space in the API,
resolved at query time by counting proposals where start <= now and
max_end > now. Display the active count in the explore page for all
protocols and sort onchain spaces by active proposals.

Depends on snapshot-labs/checkpoint#390

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix: correct resolver table name, add sql subquery for sorting

- Fix resolver to query the shared `proposals` table with `_indexer`
  filter instead of a non-existent `{indexer}_proposals` table
- Provide `sql` subquery so active_proposal_count can be used in
  ORDER BY at the database level (enables proper pagination)
- Remove client-side sort, use orderBy: active_proposal_count in the
  GraphQL query instead

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* refactor: simplify resolver to sql-only config

Drop the redundant resolve function, only provide the sql subquery.
Checkpoint auto-derives field resolution from the sql subselect result.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* refactor: drop UI changes from this PR (split to stacked UI PR)

Revert the 4 apps/ui files to base so #2125 contains only the
backend/API active_proposal_count changes. The UI half (Explore
column + graphqlApi queries) moves to a stacked UI PR.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* chore(api): bump checkpoint to 0.1.0-beta.73 for @computed support

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* style(api): fix prettier formatting in active_proposal_count resolver

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Tony <222686154+tony8713@users.noreply.github.com>
Sekhmet pushed a commit to snapshot-labs/sx-monorepo that referenced this pull request Jul 3, 2026
* feat: show active proposals count for Snapshot X and Governor spaces

Add active_proposal_count as a @computed field on Space in the API,
resolved at query time by counting proposals where start <= now and
max_end > now. Display the active count in the explore page for all
protocols and sort onchain spaces by active proposals.

Depends on snapshot-labs/checkpoint#390

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix: correct resolver table name, add sql subquery for sorting

- Fix resolver to query the shared `proposals` table with `_indexer`
  filter instead of a non-existent `{indexer}_proposals` table
- Provide `sql` subquery so active_proposal_count can be used in
  ORDER BY at the database level (enables proper pagination)
- Remove client-side sort, use orderBy: active_proposal_count in the
  GraphQL query instead

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* refactor: simplify resolver to sql-only config

Drop the redundant resolve function, only provide the sql subquery.
Checkpoint auto-derives field resolution from the sql subselect result.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* refactor: drop UI changes from this PR (split to stacked UI PR)

Revert the 4 apps/ui files to base so #2125 contains only the
backend/API active_proposal_count changes. The UI half (Explore
column + graphqlApi queries) moves to a stacked UI PR.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(ui): add active proposals Explore column

UI half split out of #2125. Adds the Active column to the Explore
view and the active_proposal_count wiring in graphqlApi queries.
Stacked on the backend PR #2125.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* revert(ui): keep Explore sort by vote_count

Ordering by active_proposal_count falls back to effectively random order
for spaces with no active proposals. Revert the Spaces query ordering to
vote_count as before, while keeping the active proposals column.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Less <fabien@bonustrack.co>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Tony <222686154+tony8713@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Wan <495709+wa0x6e@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants