Skip to content

Add TypeMap abstract class#30

Merged
FionaBronwen merged 5 commits intofeature/graphqlfrom
fionabronwen/type-map-abstraction
Jun 5, 2025
Merged

Add TypeMap abstract class#30
FionaBronwen merged 5 commits intofeature/graphqlfrom
fionabronwen/type-map-abstraction

Conversation

@FionaBronwen
Copy link

@FionaBronwen FionaBronwen commented Jun 4, 2025

Summary

#29 was getting too large 👀 , so I've started the process of breaking it up into more easily digested pieces. This PR adds a new TypeMap abstract class to be used in the GraphQLTypeRegistry.

Note: This PR does not yet add the abstract class's implementations (i.e. ModelTypeMap, EnumTypeMap, etc.).

TypeMap<T, G> Base Class

Purpose

Generic base class that provides the core registration and materialization lifecycle for mapping TypeSpec types (T) to GraphQL types (G).

Key Properties

registrationMap: Storage for TypeSpec contexts awaiting materialization
materializedMap: Cache of already-created GraphQL types

Core Methods

register(context: TSPContext<T>): string

  • Stores TypeSpec type with metadata for later materialization
  • Performs conflict detection for duplicate registrations
  • Returns the registration name used as a key

get(name: string): G | undefined

  • Returns cached GraphQL type if already materialized
  • Attempts lazy materialization if type is registered but not yet materialized
  • Caches result for future calls

Utility Methods

isRegistered(name: string): Check if a type is registered
getAllMaterialized(): G[]: Get all cached GraphQL types
reset(): Clear all registrations and cache

Abstract Methods (Subclass Responsibility)

getNameFromContext(context): Extract registration name from context
materialize(context): Create the actual GraphQL type from TypeSpec context

@FionaBronwen FionaBronwen marked this pull request as ready for review June 4, 2025 21:00
@steverice
Copy link

One thing discussed in the other PR is that we want to avoid collisions in the materialized types. If we have separate TypeMaps for different TSP types (ModelMap, EnumMap, ScalarMap, et al), where do we catch collisions among the different maps — e.g. we have a model, a scalar, and an enum all named Foo?

@FionaBronwen
Copy link
Author

One thing discussed in the other PR is that we want to avoid collisions in the materialized types. If we have separate TypeMaps for different TSP types (ModelMap, EnumMap, ScalarMap, et al), where do we catch collisions among the different maps — e.g. we have a model, a scalar, and an enum all named Foo?

I'm thinking we could keep some global state within the registry itself to track names for name collisions and then we would consult that set to check for a collision before adding new entries in each TypeMap.

@FionaBronwen FionaBronwen merged commit 763d542 into feature/graphql Jun 5, 2025
@FionaBronwen FionaBronwen deleted the fionabronwen/type-map-abstraction branch December 22, 2025 16:00
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