Skip to content

Generate per-interface class maps for type-safe Node spec support#234

Draft
captbaritone wants to merge 4 commits intomainfrom
interface-class-maps
Draft

Generate per-interface class maps for type-safe Node spec support#234
captbaritone wants to merge 4 commits intomainfrom
interface-class-maps

Conversation

@captbaritone
Copy link
Copy Markdown
Owner

Summary

  • For each GraphQL interface with exported class implementors, Grats now generates an exported typename-to-class map in schema.ts (e.g. nodeClassMap). This enables type-safe dispatch patterns like the Node spec's node() resolver — TypeScript enforces that all implementors provide the required static methods.
  • When resolveType is generated for class-based types, also exports a getTypeName function so users can derive the GraphQL typename from a class instance without defining __typename.
  • Adds a Node spec guide with copy-pasteable examples.

Test plan

  • All existing unit and integration tests pass
  • Updated resolveTypeViaClass integration test demonstrates the full pattern: functional @gqlField on interface, class map dispatch, getTypeName for ID encoding, no __typename on classes
  • Production-app example updated to use the new pattern and type-checks cleanly
  • All other example project schemas regenerated successfully

For each GraphQL interface with exported class implementors, Grats now
generates an exported typename-to-class map in schema.ts. This enables
type-safe dispatch patterns like the Node spec's `node()` resolver
without manual switch statements — TypeScript enforces that all
implementors provide the required static methods.

Also updates the production-app example to use the generated
`nodeClassMap` and the resolveTypeViaClass integration test to
demonstrate the full pattern including functional @gqlField on
interfaces.
When resolveType is generated for class-based types, also export a
getTypeName alias. This lets users derive the GraphQL typename from
a class instance without defining __typename, which is useful for
encoding global IDs in the Node spec pattern.

Updates the production-app example and resolveTypeViaClass integration
test to use getTypeName instead of __typename.
Documents how to implement GraphQL's Global Object Identification
spec using Grats' generated nodeClassMap and getTypeName exports,
with copy-pasteable code examples.
@netlify
Copy link
Copy Markdown

netlify Bot commented May 9, 2026

Deploy Preview for grats failed. Why did it fail? →

Name Link
🔨 Latest commit 6dedaa6
🔍 Latest deploy log https://app.netlify.com/projects/grats/deploys/69fe7b089d62b500073716a8

Fixes prettier formatting in schemaCodegen.ts and test.ts, and
regenerates all example project schemas to include the new
getTypeName export.
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.

1 participant