11import type { Model } from "@typespec/compiler" ;
22import {
3+ GraphQLInputObjectType ,
34 GraphQLObjectType ,
45 GraphQLString ,
56 type GraphQLFieldConfigMap ,
7+ type GraphQLInputFieldConfigMap ,
8+ type GraphQLInputType ,
9+ type GraphQLNamedType ,
610 type GraphQLOutputType ,
711} from "graphql" ;
812import { TypeMap , type TSPContext , type TypeKey } from "../type-maps.js" ;
913
1014/**
11- * TypeMap for converting TypeSpec Models to GraphQL ObjectTypes.
15+ * TypeMap for converting TypeSpec Models to GraphQL ObjectTypes or InputObjectTypes .
1216 *
1317 * Handles registration of TSP models and lazy materialization into
14- * GraphQLObjectType instances.
18+ * GraphQLObjectType (for output) or GraphQLInputObjectType (for input).
19+ * Input types are identified by names ending in "Input" (added by GraphQLModelMutation).
1520 */
16- export class ModelTypeMap extends TypeMap < Model , GraphQLObjectType > {
21+ export class ModelTypeMap extends TypeMap < Model , GraphQLNamedType > {
1722 /**
1823 * Derives the type key from the context.
1924 * Uses graphqlName override if provided, otherwise uses the model's name.
@@ -23,18 +28,31 @@ export class ModelTypeMap extends TypeMap<Model, GraphQLObjectType> {
2328 }
2429
2530 /**
26- * Materializes a TypeSpec Model into a GraphQL ObjectType.
31+ * Materializes a TypeSpec Model into a GraphQL ObjectType or InputObjectType .
2732 */
28- protected materialize ( context : TSPContext < Model > ) : GraphQLObjectType {
33+ protected materialize ( context : TSPContext < Model > ) : GraphQLNamedType {
2934 const tspModel = context . type ;
3035 const name = context . graphqlName ?? tspModel . name ;
3136
37+ // Determine if this is an input type based on name suffix
38+ const isInput = name . endsWith ( "Input" ) ;
39+
40+ if ( isInput ) {
41+ return this . materializeInputType ( tspModel , name ) ;
42+ } else {
43+ return this . materializeOutputType ( tspModel , name ) ;
44+ }
45+ }
46+
47+ /**
48+ * Materialize as a GraphQLObjectType (output type).
49+ */
50+ private materializeOutputType ( tspModel : Model , name : string ) : GraphQLObjectType {
3251 const fields : GraphQLFieldConfigMap < unknown , unknown > = { } ;
3352
3453 for ( const [ propName , prop ] of tspModel . properties ) {
3554 fields [ propName ] = {
36- type : this . mapPropertyType ( prop . type ) ,
37- // TODO: Add description from doc comments
55+ type : this . mapOutputType ( prop . type ) ,
3856 } ;
3957 }
4058
@@ -44,11 +62,38 @@ export class ModelTypeMap extends TypeMap<Model, GraphQLObjectType> {
4462 } ) ;
4563 }
4664
65+ /**
66+ * Materialize as a GraphQLInputObjectType (input type).
67+ */
68+ private materializeInputType ( tspModel : Model , name : string ) : GraphQLInputObjectType {
69+ const fields : GraphQLInputFieldConfigMap = { } ;
70+
71+ for ( const [ propName , prop ] of tspModel . properties ) {
72+ fields [ propName ] = {
73+ type : this . mapInputType ( prop . type ) ,
74+ } ;
75+ }
76+
77+ return new GraphQLInputObjectType ( {
78+ name,
79+ fields,
80+ } ) ;
81+ }
82+
4783 /**
4884 * Map a TypeSpec property type to a GraphQL output type.
4985 * TODO: Implement full type mapping with references to other registered types.
5086 */
51- private mapPropertyType ( _type : unknown ) : GraphQLOutputType {
87+ private mapOutputType ( _type : unknown ) : GraphQLOutputType {
88+ // Placeholder - will need to resolve references to other types
89+ return GraphQLString ;
90+ }
91+
92+ /**
93+ * Map a TypeSpec property type to a GraphQL input type.
94+ * TODO: Implement full type mapping with references to other registered types.
95+ */
96+ private mapInputType ( _type : unknown ) : GraphQLInputType {
5297 // Placeholder - will need to resolve references to other types
5398 return GraphQLString ;
5499 }
0 commit comments