Skip to content

Commit 803731e

Browse files
committed
feat(graphql): integrate mutation engine and TypeMaps into emitter
1 parent 5161de0 commit 803731e

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

packages/graphql/src/schema-emitter.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {
1111
import { GraphQLSchema, validateSchema } from "graphql";
1212
import { type GraphQLEmitterOptions } from "./lib.js";
1313
import type { Schema } from "./lib/schema.js";
14+
import {
15+
createGraphQLMutationEngine,
16+
type GraphQLMutationEngine,
17+
} from "./mutation-engine/index.js";
1418
import { GraphQLTypeRegistry } from "./registry.js";
1519

1620
class GraphQLSchemaEmitter {
@@ -19,26 +23,30 @@ class GraphQLSchemaEmitter {
1923
private options: GraphQLEmitterOptions;
2024
private diagnostics: DiagnosticCollector;
2125
private registry: GraphQLTypeRegistry;
26+
private engine: GraphQLMutationEngine;
27+
2228
constructor(
2329
tspSchema: Schema,
2430
context: EmitContext<GraphQLEmitterOptions>,
2531
options: GraphQLEmitterOptions,
2632
) {
27-
// Initialize any properties if needed, including the registry
2833
this.tspSchema = tspSchema;
2934
this.context = context;
3035
this.options = options;
3136
this.diagnostics = createDiagnosticCollector();
3237
this.registry = new GraphQLTypeRegistry();
38+
this.engine = createGraphQLMutationEngine(context.program, tspSchema.type);
3339
}
3440

3541
async emitSchema(): Promise<[GraphQLSchema, Readonly<Diagnostic[]>] | undefined> {
36-
const schemaNamespace = this.tspSchema.type;
37-
// Logic to emit the GraphQL schema
38-
navigateTypesInNamespace(schemaNamespace, this.semanticNodeListener());
42+
43+
// Navigate the original namespace, mutate on-demand via engine
44+
navigateTypesInNamespace(this.tspSchema.type, this.semanticNodeListener());
45+
3946
const schemaConfig = this.registry.materializeSchemaConfig();
4047
const schema = new GraphQLSchema(schemaConfig);
41-
// validate the schema
48+
49+
// Validate the schema
4250
const validationErrors = validateSchema(schema);
4351
validationErrors.forEach((error) => {
4452
this.diagnostics.add({
@@ -52,20 +60,22 @@ class GraphQLSchemaEmitter {
5260
}
5361

5462
semanticNodeListener() {
55-
// TODO: Add GraphQL types to registry as the TSP nodes are visited
5663
return {
5764
enum: (node: Enum) => {
58-
this.registry.addEnum(node);
65+
const mutation = this.engine.mutateEnum(node);
66+
this.registry.addEnum(mutation.mutatedType);
5967
},
6068
model: (node: Model) => {
61-
// TODO: Determine usageFlag from mutation engine or usage tracking
62-
this.registry.addModel(node, UsageFlags.Output);
69+
const mutation = this.engine.mutateModel(node);
70+
this.registry.addModel(mutation.mutatedType, UsageFlags.Output);
6371
},
6472
exitEnum: (node: Enum) => {
65-
this.registry.materializeEnum(node.name);
73+
const mutation = this.engine.mutateEnum(node);
74+
this.registry.materializeEnum(mutation.mutatedType.name);
6675
},
6776
exitModel: (node: Model) => {
68-
this.registry.materializeModel(node.name);
77+
const mutation = this.engine.mutateModel(node);
78+
this.registry.materializeModel(mutation.mutatedType.name);
6979
},
7080
};
7181
}
@@ -76,7 +86,6 @@ export function createSchemaEmitter(
7686
context: EmitContext<GraphQLEmitterOptions>,
7787
options: GraphQLEmitterOptions,
7888
): GraphQLSchemaEmitter {
79-
// Placeholder for creating a GraphQL schema emitter
8089
return new GraphQLSchemaEmitter(schema, context, options);
8190
}
8291

packages/graphql/test/emitter.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { strictEqual } from "node:assert";
22
import { describe, it } from "vitest";
33
import { emitSingleSchema } from "./test-host.js";
44

5+
// Expected output with models. Note: field types are placeholders (String) until
6+
// type resolution is fully implemented.
57
const expectedGraphQLSchema = `type Book {
68
name: String
79
page_count: String

packages/graphql/test/main.tsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace MyLibrary {
1919
}
2020

2121
enum Genre {
22-
Fiction,
22+
$Fiction$,
2323
NonFiction,
2424
Mystery,
2525
Fantasy,

0 commit comments

Comments
 (0)