Skip to content

fix: expose enum fields as String in where inputs - #397

Closed
bonustrack wants to merge 1 commit into
masterfrom
fix/enum-where-inputs
Closed

bonustrack wants to merge 1 commit into
masterfrom
fix/enum-where-inputs

Conversation

@bonustrack

Copy link
Copy Markdown
Member

Summary

When an entity field is typed as a GraphQL enum, the generated where input fields (field, field_not, field_in, field_not_in) are now typed as String instead of the enum. The output field keeps its enum type (validation and introspection intact).

Why

Consumers that codegen their types from the query schema (e.g. the sx-monorepo UI) construct filter objects in existing code. If tightening an entity field from String to an enum also tightens the filter input, every downstream client is forced to adapt its filter code in lockstep with the schema change. Typing filters as String keeps filter shapes stable (and matches hub API filter semantics) while the response side still gets the fully typed enum.

Concrete motivation: adding state: ProposalState! @computed to the sx-monorepo API (snapshot-labs/sx-monorepo#2226) without breaking the UI's regenerated Proposal_filter types.

Test plan

  • New unit test snapshot: entity with an enum field generates state: ProposalState! on the type and state: String / state_not / state_in / state_not_in in the filter input
  • Full jest suite passes (23 snapshots)
  • Verified end-to-end in sx-monorepo: enum output field, string-valued filters, orderBy on the computed enum field, against a live Postgres

🤖 Generated with Claude Code

Keeps generated filter input shapes stable for clients when an entity
field is tightened from String to an enum, matching hub API filter
semantics. The output field keeps its enum type.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@bonustrack
bonustrack force-pushed the fix/enum-where-inputs branch from 92999f2 to a920710 Compare July 11, 2026 15:58
bonustrack added a commit to snapshot-labs/sx-monorepo that referenced this pull request Jul 11, 2026
Filters stay String-typed so codegen'd client filter shapes are
unaffected, via a temporary patch of checkpoint (snapshot-labs/checkpoint#397).
Remove the patch and bump checkpoint once it is released.

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

@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.

Not sure I understand why we want to change it?

With enums we will get correct typing for inputs:

Image

With string we just make it accept anything. I'd imagine that if someone generates their client side query types based on schema they expect it to give them benefit of good types, not as loose as possible.

Maybe there is some benefit to this (I can't find a real benefit from PR description), but it's definitely not a "fix".

I see some reference to ProposalState change than forces us to do it, but I don't see why we don't just do that, clean and type-safe:

diff --git a/apps/ui/src/networks/common/graphqlApi/index.ts b/apps/ui/src/networks/common/graphqlApi/index.ts
index 9545c65bd..e0c26991c 100644
--- a/apps/ui/src/networks/common/graphqlApi/index.ts
+++ b/apps/ui/src/networks/common/graphqlApi/index.ts
@@ -643,30 +643,15 @@ export function createApi(
       filters?: ProposalsFilter,
       searchQuery = ''
     ): Promise<Proposal[]> => {
-      const _filters: ProposalsFilter = clone(filters || {});
+      const { state, labels, ...restFilters } = filters || {};
 
       const metadataFilters: Record<string, any> = {};
       if (searchQuery) metadataFilters.title_contains_nocase = searchQuery;
 
-      const state = _filters.state;
-
-      if (state === 'active') {
-        _filters.start_lte = current;
-        _filters.max_end_gte = current;
-      } else if (state === 'pending') {
-        _filters.start_gt = current;
-      } else if (state === 'closed') {
-        _filters.max_end_lt = current;
+      if (labels?.length) {
+        metadataFilters.labels_contains = labels;
       }
 
-      delete _filters.state;
-
-      if (_filters.labels?.length) {
-        metadataFilters.labels_contains = _filters.labels;
-      }
-
-      delete _filters.labels;
-
       const { data } = await apollo.query({
         query: PROPOSALS_QUERY,
         variables: {
@@ -675,10 +660,11 @@ export function createApi(
           where: {
             space_in: spaceIds,
             cancelled: false,
+            state: state !== 'any' ? state : undefined,
             metadata_: Object.keys(metadataFilters).length
               ? metadataFilters
               : undefined,
-            ..._filters
+            ...restFilters
           }
         }
       });

@Sekhmet

Sekhmet commented Jul 16, 2026

Copy link
Copy Markdown
Member

This has been addressed at consumer side: snapshot-labs/sx-monorepo#2226

@Sekhmet Sekhmet closed this Jul 16, 2026
@bonustrack
bonustrack deleted the fix/enum-where-inputs branch July 16, 2026 09:08
bonustrack added a commit to snapshot-labs/sx-monorepo that referenced this pull request Jul 22, 2026
* feat(api): add computed state field to proposals

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

* feat(api): type proposal state as enum

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

* fix(ui): keep proposals state filter out of typed where input

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

* revert: drop state enum and UI changes, keep state as String

Enum type in the API schema forces regenerated UI filter types to reject
the UI's existing state values, requiring UI changes. Keep state as a
String so this PR stays API-only.

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

* feat(api): restore ProposalState enum for proposal state

The enum tightens the codegen'd UI filter types, so the UI filter
construction is adjusted type-only (destructure instead of delete).
No UI runtime behavior or query changes.

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

* revert: keep PR API-only, state as String

The ProposalState enum will come in a follow-up PR together with the
UI adopting server-side state filtering, after the API is deployed.

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

* feat(api): type proposal state as ProposalState enum

Filters stay String-typed so codegen'd client filter shapes are
unaffected, via a temporary patch of checkpoint (snapshot-labs/checkpoint#397).
Remove the patch and bump checkpoint once it is released.

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

* chore: drop checkpoint patch, use released checkpoint once available

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

* chore: restore bun.lock from master

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

* fix(ui): make proposals state filter type-safe via rest operator

Destructure state/labels out of the filters instead of clone + delete so
the spread into Proposal_filter no longer carries the enum-incompatible
'any' state value. Keeps the checkpoint-generated enum filters as-is.

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Wan <495709+wa0x6e@users.noreply.github.com>
Co-authored-by: Tony <222686154+tony8713@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.

2 participants