Generate per-interface class maps for type-safe Node spec support#234
Draft
captbaritone wants to merge 4 commits intomainfrom
Draft
Generate per-interface class maps for type-safe Node spec support#234captbaritone wants to merge 4 commits intomainfrom
captbaritone wants to merge 4 commits intomainfrom
Conversation
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.
❌ Deploy Preview for grats failed. Why did it fail? →
|
Fixes prettier formatting in schemaCodegen.ts and test.ts, and regenerates all example project schemas to include the new getTypeName export.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
schema.ts(e.g.nodeClassMap). This enables type-safe dispatch patterns like the Node spec'snode()resolver — TypeScript enforces that all implementors provide the required static methods.resolveTypeis generated for class-based types, also exports agetTypeNamefunction so users can derive the GraphQL typename from a class instance without defining__typename.Test plan
resolveTypeViaClassintegration test demonstrates the full pattern: functional@gqlFieldon interface, class map dispatch,getTypeNamefor ID encoding, no__typenameon classes