Skip to content

Commit 2ef9793

Browse files
committed
Remove modelVariants workaround from components
The mutation engine now handles all input/output type naming, so components can use type.name directly without looking up whether a model has both input and output variants.
1 parent 73b8a3a commit 2ef9793

3 files changed

Lines changed: 14 additions & 30 deletions

File tree

packages/graphql/src/components/fields/type-expression.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from "@typespec/compiler";
88
import { type Children } from "@alloy-js/core";
99
import { useTsp } from "@typespec/emitter-framework";
10-
import { useGraphQLSchema } from "../../context/index.js";
1110
import { isNullable } from "../../lib/nullable.js";
1211
import { unwrapNullableUnion, getUnionName } from "../../lib/type-utils.js";
1312
import { getGraphQLBuiltinName, getScalarMapping } from "../../lib/scalar-mappings.js";
@@ -42,7 +41,6 @@ export interface GraphQLTypeExpressionProps {
4241

4342
export function GraphQLTypeExpression(props: GraphQLTypeExpressionProps) {
4443
const { $, program } = useTsp();
45-
const { modelVariants } = useGraphQLSchema();
4644

4745
const nullable = props.isNullable || isNullable(props.type);
4846

@@ -145,14 +143,8 @@ export function GraphQLTypeExpression(props: GraphQLTypeExpressionProps) {
145143
}
146144

147145
if ($.model.is(type)) {
148-
// Both input and output variants share the same source model identity,
149-
// so we use name-based lookup to determine if both variants exist.
150-
const hasOutputVariant = modelVariants.outputModels.has(type.name);
151-
const hasInputVariant = modelVariants.inputModels.has(type.name);
152-
153-
if (props.isInput && hasOutputVariant && hasInputVariant) {
154-
return `${type.name}Input`;
155-
}
146+
// The mutation engine handles input/output naming - input models are
147+
// already suffixed with "Input" when they need to be distinguished.
156148
return type.name;
157149
}
158150

packages/graphql/src/components/types/input-type.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Model } from "@typespec/compiler";
22
import * as gql from "@alloy-js/graphql";
33
import { useTsp } from "@typespec/emitter-framework";
4-
import { useGraphQLSchema } from "../../context/index.js";
54
import { Field } from "../fields/index.js";
65

76
export interface InputTypeProps {
@@ -12,22 +11,16 @@ export interface InputTypeProps {
1211
/**
1312
* Renders a GraphQL input type declaration
1413
*
15-
* Determines the correct input type name:
16-
* - If the model is also an output type, appends "Input"
17-
* - Otherwise, uses the name as-is
14+
* The mutation engine handles naming: input models are already suffixed
15+
* with "Input" when they need to be distinguished from output types.
1816
*/
1917
export function InputType(props: InputTypeProps) {
2018
const { $ } = useTsp();
21-
const { modelVariants } = useGraphQLSchema();
2219
const doc = $.type.getDoc(props.type);
2320
const properties = Array.from(props.type.properties.values());
2421

25-
// If there's an output variant with the same name, add Input suffix
26-
const hasOutputVariant = modelVariants.outputModels.has(props.type.name);
27-
const inputTypeName = hasOutputVariant ? `${props.type.name}Input` : props.type.name;
28-
2922
return (
30-
<gql.InputObjectType name={inputTypeName} description={doc}>
23+
<gql.InputObjectType name={props.type.name} description={doc}>
3124
{properties.map((prop) => (
3225
<Field property={prop} isInput={true} />
3326
))}

packages/graphql/test/components/input-type.test.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,21 @@ describe("InputType component", () => {
4949
expect(sdl).toContain("bio: String!");
5050
});
5151

52-
it("appends Input suffix when model has an output variant", async () => {
53-
const { Pet } = await tester.compile(
54-
t.code`model ${t.model("Pet")} { name: string; }`,
52+
it("renders mutated input model with Input suffix from mutation engine", async () => {
53+
// The mutation engine adds the Input suffix when a model is used as input.
54+
// This test simulates that by using a model already named with Input suffix.
55+
const { PetInput } = await tester.compile(
56+
t.code`model ${t.model("PetInput")} { name: string; }`,
5557
);
5658

57-
const sdl = renderComponentToSDL(tester.program, <InputType type={Pet} />, {
58-
modelVariants: {
59-
outputModels: new Map([["Pet", Pet]]),
60-
inputModels: new Map([["Pet", Pet]]),
61-
},
62-
});
59+
const sdl = renderComponentToSDL(tester.program, <InputType type={PetInput} />);
6360

6461
expect(sdl).toContain("input PetInput {");
6562
});
6663

67-
it("uses original name when no output variant exists", async () => {
64+
it("renders input-only model without suffix", async () => {
65+
// Models used only as inputs (never as outputs) don't need the Input suffix.
66+
// The mutation engine handles this - it only adds suffix when needed.
6867
const { CreatePet } = await tester.compile(
6968
t.code`model ${t.model("CreatePet")} { name: string; }`,
7069
);

0 commit comments

Comments
 (0)