Skip to content

[WIP] Rearchitect GraphQL Emitter to use Alloy.js component-based approach#61

Draft
FionaBronwen wants to merge 17 commits intofionabronwen/graphql-mutation-engine-v2from
fionabronwen/graphql-component-emitter
Draft

[WIP] Rearchitect GraphQL Emitter to use Alloy.js component-based approach#61
FionaBronwen wants to merge 17 commits intofionabronwen/graphql-mutation-engine-v2from
fionabronwen/graphql-component-emitter

Conversation

@FionaBronwen
Copy link

@FionaBronwen FionaBronwen commented Feb 23, 2026

Summary

  • Replaces previous approach with declarative JSX component tree powered by
    @alloy-js/core and @alloy-js/graphql
  • Delete the old emitter pipeline (emitter.ts, graphql-emitter.ts, schema-emitter.ts, registry.ts, type-maps.ts) and replace with
    composable <GraphQLSchema>, <ObjectType>, <InputType>, <Field>, etc. components
  • Add visibility-based input/output type splitting, automatic nullable union unwrapping, and scalar mapping integration built on the
    mutation engine from previous PR
  • Add 153 new tests covering e2e emission, design doc examples, nullability, input/output splitting, scalar mappings, diagnostics, and coverage gaps

Why Alloy.js?

The previous emitter used an imperative visitor pattern that manually constructed GraphQL AST nodes. This was difficult to extend and didn't make use of the TSP Emitter Framework. Alloy.js lets us express the GraphQL schema as a component tree where each TypeSpec type maps to a JSX component (<ObjectType>, <EnumType>, <UnionType>, etc.).

The @alloy-js/graphql package handles SDL rendering, so our components focus purely on the TypeSpec-to-GraphQL mapping logic. This makes the emitter easier to read, test, and extend.

Want to run this branch locally?

The @typespec/graphql package depends on a local link to the Alloy repo via
pnpm overrides in pnpm-workspace.yaml. Both repos need to be siblings in the
same parent directory. The GraphQL emitter uses Alloy's graphql-js bindings,
which live on the feature/graphql-js branch in Alloy.

Clone both repos as siblings

git clone git@github.com:pinterest/typespec.git
git clone https://github.com/pinterest/alloy.git

Alloy: check out the graphql-js bindings branch

cd alloy && git checkout feature/graphql-js
pnpm install && pnpm build:all
cd ..

TypeSpec: check out the component emitter branch

cd typespec && git checkout fionabronwen/graphql-component-emitter
pnpm install && pnpm -C packages/graphql run build

Run the emitter against the sample schema

pnpm -C packages/graphql tsp compile test/main.tsp --emit @typespec/graphql
Output schema in: packages/graphql/tsp-output/@typespec/graphql/schema.graphql

## Summary

Adds utility functions for transforming TypeSpec names into valid GraphQL identifiers. These utilities form the foundation for name handling throughout the GraphQL emitter.

## Changes

- **`src/lib/type-utils.ts`** - Core utility functions for GraphQL name transformations
- **`test/lib/type-utils.test.ts`** - Unit tests for `sanitizeNameForGraphQL`

## Utilities Added

| Function | Purpose |
|----------|---------|
| `sanitizeNameForGraphQL` | Sanitize names to be valid GraphQL identifiers |
| `toTypeName` | Convert to PascalCase for type names |
| `toFieldName` | Convert to camelCase for field names |
| `toEnumMemberName` | Convert to CONSTANT_CASE for enum members |
| `getUnionName` | Generate names for anonymous unions |
| `getTemplatedModelName` | Generate names for templated models (e.g., `ListOfString`) |
| `isArray`, `isRecordType` | Type guards for array/record models |
| `unwrapModel`, `unwrapType` | Extract element types from arrays |
| `isTrueModel` | Check if a model should emit as GraphQL object type |
| `getGraphQLDoc` | Extract doc comments for GraphQL descriptions |
@FionaBronwen FionaBronwen force-pushed the fionabronwen/graphql-component-emitter branch from aad8f78 to 14c0688 Compare February 23, 2026 21:35
@FionaBronwen FionaBronwen force-pushed the fionabronwen/graphql-mutation-engine-v2 branch 3 times, most recently from 4ab36ef to c4d5642 Compare February 25, 2026 21:50
Introduce a mutation engine that transforms TypeSpec types into
GraphQL-compatible forms using the mutator framework. Includes mutations
for enums, models, scalars, unions, and operations.

Also includes package hygiene: add tspMain, update node engine to >=20,
add api-extractor.json, CHANGELOG.md, fix testing casing, and clean up
dead code.
Operations automatically propagate input context to parameters and
output context to return types via GraphQLMutationOptions. The
framework's cache and options propagation handle nested types, so the
same source model produces separate input and output mutations without
any custom type-graph walking.
@FionaBronwen FionaBronwen force-pushed the fionabronwen/graphql-mutation-engine-v2 branch from a05a236 to 79ea773 Compare February 26, 2026 16:15
@FionaBronwen FionaBronwen force-pushed the fionabronwen/graphql-component-emitter branch from 14c0688 to a0869c7 Compare February 26, 2026 20:17
FionaBronwen and others added 13 commits February 27, 2026 13:34
Co-authored-by: Steve Rice <srice@pinterest.com>
Co-authored-by: Steve Rice <srice@pinterest.com>
Co-authored-by: Steve Rice <srice@pinterest.com>
Co-authored-by: Steve Rice <srice@pinterest.com>
Co-authored-by: Steve Rice <srice@pinterest.com>
Rewrite the GraphQL emitter using @alloy-js/graphql components and
@typespec/emitter-framework for program context management. The new
architecture uses JSX components for each GraphQL type (object, input,
enum, scalar, union, interface) with a context-based system for type
resolution and field analysis.

Key changes:
- Add Alloy.js component tree for GraphQL SDL generation
- Add emitter-framework TspContext for program access via useTsp()
- Add visibility-based input/output type classification
- Replace old emitter.ts/graphql-emitter.ts/schema-emitter.ts with
  component-based emitter.tsx
- Add comprehensive test suite (184 tests)
Dissolve design-doc-examples.test.ts, coverage-gaps.test.ts,
emitter.test.ts, and diagnostics.test.ts — distribute their tests
into feature-focused files. Create new files for unions, enums,
arrays, deprecation, circular references, and doc comments.

197 tests across 17 files (was 197 across 15).
@FionaBronwen FionaBronwen force-pushed the fionabronwen/graphql-component-emitter branch from 75dca02 to 8b85d6f Compare March 3, 2026 23:31
@FionaBronwen FionaBronwen changed the title Rearchitect GraphQL Emitter to use Alloy.js component-based approach [WIP] Rearchitect GraphQL Emitter to use Alloy.js component-based approach Mar 3, 2026
@FionaBronwen FionaBronwen force-pushed the fionabronwen/graphql-mutation-engine-v2 branch from 70e7589 to 84b7764 Compare March 13, 2026 18:53
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.

1 participant