|
| 1 | +import { UsageFlags, type Model } from "@typespec/compiler"; |
| 2 | +import { |
| 3 | + GraphQLInputObjectType, |
| 4 | + GraphQLObjectType, |
| 5 | + GraphQLString, |
| 6 | + type GraphQLFieldConfigMap, |
| 7 | + type GraphQLInputFieldConfigMap, |
| 8 | +} from "graphql"; |
| 9 | +import { TypeMap, type TSPContext, type TypeKey } from "../type-maps.js"; |
| 10 | + |
| 11 | +/** |
| 12 | + * TypeMap for converting TypeSpec Models to GraphQL ObjectTypes or InputObjectTypes. |
| 13 | + * |
| 14 | + * Handles registration of TSP models and lazy materialization into |
| 15 | + * GraphQLObjectType (for output) or GraphQLInputObjectType (for input) instances. |
| 16 | + * The usageFlag in TSPContext determines which type to create. |
| 17 | + */ |
| 18 | +export class ModelTypeMap extends TypeMap<Model, GraphQLObjectType | GraphQLInputObjectType> { |
| 19 | + /** |
| 20 | + * Derives the type key from the mutated model's name. |
| 21 | + */ |
| 22 | + protected getNameFromContext(context: TSPContext<Model>): TypeKey { |
| 23 | + return context.type.name as TypeKey; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Materializes a TypeSpec Model into a GraphQL ObjectType or InputObjectType. |
| 28 | + */ |
| 29 | + protected materialize(context: TSPContext<Model>): GraphQLObjectType | GraphQLInputObjectType { |
| 30 | + const tspModel = context.type; |
| 31 | + const name = tspModel.name; |
| 32 | + |
| 33 | + // Create InputObjectType for input usage, ObjectType for output |
| 34 | + if (context.usageFlag === UsageFlags.Input) { |
| 35 | + return this.materializeInputType(name, tspModel); |
| 36 | + } |
| 37 | + return this.materializeOutputType(name, tspModel); |
| 38 | + } |
| 39 | + |
| 40 | + private materializeOutputType(name: string, tspModel: Model): GraphQLObjectType { |
| 41 | + const fields: GraphQLFieldConfigMap<unknown, unknown> = {}; |
| 42 | + |
| 43 | + for (const [propName, prop] of tspModel.properties) { |
| 44 | + fields[propName] = { |
| 45 | + type: this.mapPropertyType(prop.type), |
| 46 | + // TODO: Add description from doc comments |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + return new GraphQLObjectType({ name, fields }); |
| 51 | + } |
| 52 | + |
| 53 | + private materializeInputType(name: string, tspModel: Model): GraphQLInputObjectType { |
| 54 | + const fields: GraphQLInputFieldConfigMap = {}; |
| 55 | + |
| 56 | + for (const [propName, prop] of tspModel.properties) { |
| 57 | + fields[propName] = { |
| 58 | + type: this.mapPropertyType(prop.type), |
| 59 | + // TODO: Add description from doc comments |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + return new GraphQLInputObjectType({ name, fields }); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Map a TypeSpec property type to a GraphQL String type. |
| 68 | + * TODO: Implement full type mapping with references to other registered types. |
| 69 | + */ |
| 70 | + private mapPropertyType(_type: unknown): typeof GraphQLString { |
| 71 | + // Placeholder - will need to resolve references to other types |
| 72 | + return GraphQLString; |
| 73 | + } |
| 74 | +} |
0 commit comments