Skip to content

Commit 664706d

Browse files
committed
Fix schema-mutator to mutate models separately for input/output contexts
Models used as both input and output must be mutated separately: - Output context produces 'Foo' - Input context produces 'FooInput' Previously, all models were mutated with Output context only, causing duplicate type names when rendering both variants.
1 parent 9d43c58 commit 664706d

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

packages/graphql/src/mutation-engine/schema-mutator.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ export function mutateSchema(
172172
}
173173
};
174174

175-
const classifyModel = (originalModel: Model, mutatedModel: Model): void => {
175+
const classifyModel = (originalModel: Model): void => {
176176
// @Interface is checked on the original (pre-clone) model, since decorator
177177
// state is stored against original type identity, not mutated clones.
178178
if (isInterface(program, originalModel)) {
179-
interfaces.push(mutatedModel);
179+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Output);
180+
interfaces.push(mutation.mutatedType);
180181
return;
181182
}
182183

@@ -189,11 +190,20 @@ export function mutateSchema(
189190
// that namespace-declared models still appear in the schema. Without
190191
// this, a model declared in the schema namespace but never referenced
191192
// by a query/mutation would silently disappear.
192-
outputModels.push(mutatedModel);
193+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Output);
194+
outputModels.push(mutation.mutatedType);
193195
return;
194196
}
195-
if (usedAsOutput) outputModels.push(mutatedModel);
196-
if (usedAsInput) inputModels.push(mutatedModel);
197+
198+
// Mutate separately for each context - input models get "Input" suffix
199+
if (usedAsOutput) {
200+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Output);
201+
outputModels.push(mutation.mutatedType);
202+
}
203+
if (usedAsInput) {
204+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Input);
205+
inputModels.push(mutation.mutatedType);
206+
}
197207
};
198208

199209
const classifyOperation = (op: Operation): void => {
@@ -215,8 +225,7 @@ export function mutateSchema(
215225
model: (node: Model) => {
216226
if (isArrayModelType(program, node)) return;
217227
if (typeUsage.isUnreachable(node)) return;
218-
const mutation = engine.mutateModel(node, GraphQLTypeContext.Output);
219-
classifyModel(node, mutation.mutatedType);
228+
classifyModel(node);
220229
visitedModelOriginals.push(node);
221230
},
222231
enum: (node: Enum) => {

0 commit comments

Comments
 (0)