Skip to content

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

Draft
captbaritone wants to merge 5 commits into
mainfrom
interface-class-maps
Draft

Generate per-interface class maps for type-safe Node spec support#234
captbaritone wants to merge 5 commits into
mainfrom
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

netlify Bot commented May 9, 2026

Copy link
Copy Markdown

Deploy Preview for grats ready!

Name Link
🔨 Latest commit 135a092
🔍 Latest deploy log https://app.netlify.com/projects/grats/deploys/6a1c732f50a5670008906616
😎 Deploy Preview https://deploy-preview-234--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.

Fixes prettier formatting in schemaCodegen.ts and test.ts, and
regenerates all example project schemas to include the new
getTypeName export.
The build.sh check that detects uncommitted changes was failing because
01-types-implement-interface.out didn't reflect the new getTypeName and
personClassMap exports, and llm-docs/guides/node-spec.md was using a
different list-bullet style than the rest of llm-docs.
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