Skip to content

Support deriving @gqlEnum from const arrays and const objects#225

Merged
captbaritone merged 3 commits intomainfrom
const-enum-support
Apr 1, 2026
Merged

Support deriving @gqlEnum from const arrays and const objects#225
captbaritone merged 3 commits intomainfrom
const-enum-support

Conversation

@captbaritone
Copy link
Copy Markdown
Owner

Summary

  • Adds support for deriving @gqlEnum from const arrays ((typeof X)[number]) and const objects ((typeof X)[keyof typeof X]), allowing runtime-accessible enum values without using TypeScript's enum syntax.
  • The const declaration must be the immediately preceding statement before the type alias. This avoids introducing name resolution into the Extractor and ensures it's clear which declarations contribute to the schema.
  • Const objects support descriptions and @deprecated on properties. Const arrays share the same limitation as union-of-literal enums (no per-value metadata).
  • Fixes TypeContext.findSymbolDeclaration to prefer declarations registered in the GraphQL schema when a symbol has multiple declarations (needed for the const X + type X same-name pattern).

Closes #205. Supersedes #215.

Test plan

  • 19 new fixture tests covering both patterns:
    • Happy paths: basic array, basic object, object with different keys/values, as const satisfies, namespace scope, function scope, descriptions, @deprecated
    • Error cases: empty, duplicate values, non-string values, invalid GraphQL names, missing as const, const not preceding, name mismatch
  • Verified empty/duplicate errors are caught by GraphQL schema validation (no redundant checks)
  • Verified graphQLNameValidationMessage is needed (graphql-js throws unlocated errors without it)
  • Full test suite passes (lint, build, all 3 test suites)

Add support for two new patterns for defining GraphQL enums with
runtime-accessible values:

- Const array: `(typeof VALUES)[number]`
- Const object: `(typeof OBJ)[keyof typeof OBJ]`

The const declaration must be the immediately preceding statement before
the @gqlEnum type alias, ensuring it's clear which declarations
contribute to the GraphQL schema. This avoids introducing name
resolution into the Extractor.

Const objects support descriptions and @deprecated on properties. Const
arrays share the same limitation as union-of-literal enums (no per-value
metadata), since TypeScript doesn't attach JSDoc to array elements.

Also fixes TypeContext.findSymbolDeclaration to prefer declarations
registered in the GraphQL schema when a symbol has multiple declarations
(needed for the `const X` + `type X` same-name pattern).

Closes #205. Supersedes #215.
@netlify
Copy link
Copy Markdown

netlify bot commented Apr 1, 2026

Deploy Preview for grats ready!

Name Link
🔨 Latest commit 05051fd
🔍 Latest deploy log https://app.netlify.com/projects/grats/deploys/69cd9fc3a228d100084857a2
😎 Deploy Preview https://deploy-preview-225--grats.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Replace inline code blocks with compiled GratsCode snippets so the
examples are validated by the Grats compiler. Also add a comment to the
array-with-description test fixture explaining that descriptions on
array elements are silently ignored.
@captbaritone captbaritone merged commit 8ccc381 into main Apr 1, 2026
14 checks passed
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.

Additional syntax for reusing variable statements

1 participant