forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 1
Add GraphQLTSPDenormalizer to GraphQL Emitter #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
FionaBronwen
wants to merge
4
commits into
feature/graphql
Choose a base branch
from
fionabronwen/denormalization
base: feature/graphql
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import type { Model, ModelProperty, Namespace, Type } from "@typespec/compiler"; | ||
| import { UsageFlags, resolveUsages, type EmitContext, type UsageTracker } from "@typespec/compiler"; | ||
| import { $ } from "@typespec/compiler/typekit"; | ||
|
|
||
| /** | ||
| * Provides utilities to denormalize TypeSpec (TSP) model types for GraphQL emitters. | ||
| * Optionally, a debug flag will print a mapping of original models to their denormalized variants. | ||
| * | ||
| * Example usage: | ||
| * ```typescript | ||
| * const denormalizer = new GraphQLTSPDenormalizer(namespace, context); | ||
| * denormalizer.denormalize(true); // with debug output | ||
| * ``` | ||
| */ | ||
| export class GraphQLTSPDenormalizer { | ||
| private usageTracker: UsageTracker; | ||
| private namespace: Namespace; | ||
| private context: EmitContext<Record<string, never>>; | ||
|
|
||
| constructor(namespace: Namespace, context: EmitContext<Record<string, never>>) { | ||
| this.namespace = namespace; | ||
| this.context = context; | ||
| this.usageTracker = resolveUsages(namespace); | ||
| } | ||
|
|
||
| denormalize(debug: boolean = false): void { | ||
| for (const [_, model] of this.namespace.models) { | ||
| this.expandInputOutputTypes(model, debug); | ||
| // TODO: Call methods for additional denormalization steps such as resolving decorators, de-anonymizing unions, etc. | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates an input variant for a model if it's used as input (e.g., User -> UserInput). | ||
| * Recursively processes nested models. Mutates namespace in-place. | ||
| * Throws on name collisions. | ||
| */ | ||
| expandInputOutputTypes(model: Model, debug: boolean) { | ||
| const typekit = $(this.context.program); | ||
| // Only process if this model is used as input | ||
| if (!this.usageTracker.isUsedAs(model, UsageFlags.Input)) return; | ||
| const inputName = model.name + "Input"; | ||
| if (this.namespace.models.has(inputName)) { | ||
| throw new Error(`Model name collision: ${inputName} already exists in namespace.`); | ||
| } | ||
| // Recursively transform nested model types to their input variants | ||
| const getInputType = (type: Type): Type => { | ||
| if (type.kind === "Model" && this.usageTracker.isUsedAs(type, UsageFlags.Input)) { | ||
| const nestedInputName = type.name + "Input"; | ||
| if (this.namespace.models.has(nestedInputName)) { | ||
| return this.namespace.models.get(nestedInputName)!; | ||
| } | ||
|
|
||
| // Create placeholder model first to prevent recursive creation | ||
| const placeholderModel = typekit.model.create({ | ||
| name: nestedInputName, | ||
| properties: {}, | ||
| }); | ||
| this.namespace.models.set(nestedInputName, placeholderModel); | ||
|
|
||
| // Now populate the properties with recursive transformation | ||
| const inputProperties: Record<string, ModelProperty> = {}; | ||
| for (const [name, prop] of type.properties) { | ||
| inputProperties[name] = typekit.modelProperty.create({ | ||
| name: prop.name, | ||
| type: getInputType(prop.type), | ||
| optional: prop.optional, | ||
| }); | ||
| } | ||
|
|
||
| // Create the final input model with all properties | ||
| const inputModel = typekit.model.create({ | ||
| name: nestedInputName, | ||
| properties: inputProperties, | ||
| }); | ||
|
|
||
| // Replace the placeholder with the fully populated model | ||
| this.namespace.models.set(nestedInputName, inputModel); | ||
| for (const [_, prop] of inputModel.properties) { | ||
| (prop as any).model = inputModel; | ||
| } | ||
|
|
||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| `[GraphQLDenormalizer] Created input model: ${type.name} -> ${nestedInputName}`, | ||
| ); | ||
| } | ||
| return inputModel; | ||
| } | ||
| return type; | ||
| }; | ||
| const inputModel = this.createInputModelVariant(model, typekit, getInputType); | ||
| this.namespace.models.set(inputName, inputModel); | ||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`[GraphQLDenormalizer] Created input model: ${model.name} -> ${inputName}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates an input model variant with transformed properties. | ||
| * Uses getInputType to recursively transform nested model references. | ||
| */ | ||
| private createInputModelVariant( | ||
| outputModel: Model, | ||
| typekit: ReturnType<typeof $>, | ||
| getInputType: (type: Type) => Type, | ||
| ): Model { | ||
| const inputProperties: Record<string, ModelProperty> = {}; | ||
| for (const [name, prop] of outputModel.properties) { | ||
| inputProperties[name] = typekit.modelProperty.create({ | ||
| name: prop.name, | ||
| type: getInputType(prop.type), | ||
| optional: prop.optional, | ||
| }); | ||
| } | ||
| const inputModel = typekit.model.create({ | ||
| name: outputModel.name + "Input", | ||
| properties: inputProperties, | ||
| }); | ||
| for (const [_, prop] of inputModel.properties) { | ||
| (prop as any).model = inputModel; | ||
| } | ||
| return inputModel; | ||
| } | ||
|
|
||
| // TODO: Add methods for additional denormalization steps such as resolving decorators, de-anonymizing unions, etc. | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a classic visitor pattern problem.
We should define a series of denormalizations as visitors that can be applied to the TypeSpec program.
TypeSpec is already essentially structured for this using
navigateProgramand friends.But I'm not 100% sure how this interacts with TypeKit, so let me know if I am missing something there cc @swatkatz