Skip to content

Commit f7a355e

Browse files
committed
feat(graphql): add ModelTypeMap for GraphQL object type materialization
1 parent 2400d78 commit f7a355e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { TypeMap, type TSPContext, type TypeKey } from "../type-maps.js";
22
export { EnumTypeMap } from "./enum.js";
3+
export { ModelTypeMap } from "./model.js";
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { Model } from "@typespec/compiler";
2+
import {
3+
GraphQLObjectType,
4+
GraphQLString,
5+
type GraphQLFieldConfigMap,
6+
type GraphQLOutputType,
7+
} from "graphql";
8+
import { TypeMap, type TSPContext, type TypeKey } from "../type-maps.js";
9+
10+
/**
11+
* TypeMap for converting TypeSpec Models to GraphQL ObjectTypes.
12+
*
13+
* Handles registration of TSP models and lazy materialization into
14+
* GraphQLObjectType instances.
15+
*/
16+
export class ModelTypeMap extends TypeMap<Model, GraphQLObjectType> {
17+
/**
18+
* Derives the type key from the context.
19+
* Uses graphqlName override if provided, otherwise uses the model's name.
20+
*/
21+
protected getNameFromContext(context: TSPContext<Model>): TypeKey {
22+
return (context.graphqlName ?? context.type.name) as TypeKey;
23+
}
24+
25+
/**
26+
* Materializes a TypeSpec Model into a GraphQL ObjectType.
27+
*/
28+
protected materialize(context: TSPContext<Model>): GraphQLObjectType {
29+
const tspModel = context.type;
30+
const name = context.graphqlName ?? tspModel.name;
31+
32+
const fields: GraphQLFieldConfigMap<unknown, unknown> = {};
33+
34+
for (const [propName, prop] of tspModel.properties) {
35+
fields[propName] = {
36+
type: this.mapPropertyType(prop.type),
37+
// TODO: Add description from doc comments
38+
};
39+
}
40+
41+
return new GraphQLObjectType({
42+
name,
43+
fields,
44+
});
45+
}
46+
47+
/**
48+
* Map a TypeSpec property type to a GraphQL output type.
49+
* TODO: Implement full type mapping with references to other registered types.
50+
*/
51+
private mapPropertyType(_type: unknown): GraphQLOutputType {
52+
// Placeholder - will need to resolve references to other types
53+
return GraphQLString;
54+
}
55+
}

0 commit comments

Comments
 (0)