Skip to content

Commit 73b6a36

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 ba72c66 commit 73b6a36

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
@@ -187,11 +187,12 @@ export function mutateSchema(
187187
}
188188
};
189189

190-
const classifyModel = (originalModel: Model, mutatedModel: Model): void => {
190+
const classifyModel = (originalModel: Model): void => {
191191
// @Interface is checked on the original (pre-clone) model, since decorator
192192
// state is stored against original type identity, not mutated clones.
193193
if (isInterface(program, originalModel)) {
194-
interfaces.push(mutatedModel);
194+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Output);
195+
interfaces.push(mutation.mutatedType);
195196
return;
196197
}
197198

@@ -204,11 +205,20 @@ export function mutateSchema(
204205
// that namespace-declared models still appear in the schema. Without
205206
// this, a model declared in the schema namespace but never referenced
206207
// by a query/mutation would silently disappear.
207-
outputModels.push(mutatedModel);
208+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Output);
209+
outputModels.push(mutation.mutatedType);
208210
return;
209211
}
210-
if (usedAsOutput) outputModels.push(mutatedModel);
211-
if (usedAsInput) inputModels.push(mutatedModel);
212+
213+
// Mutate separately for each context - input models get "Input" suffix
214+
if (usedAsOutput) {
215+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Output);
216+
outputModels.push(mutation.mutatedType);
217+
}
218+
if (usedAsInput) {
219+
const mutation = engine.mutateModel(originalModel, GraphQLTypeContext.Input);
220+
inputModels.push(mutation.mutatedType);
221+
}
212222
};
213223

214224
const classifyOperation = (op: Operation): void => {
@@ -230,8 +240,7 @@ export function mutateSchema(
230240
model: (node: Model) => {
231241
if (isArrayModelType(program, node)) return;
232242
if (typeUsage.isUnreachable(node)) return;
233-
const mutation = engine.mutateModel(node, GraphQLTypeContext.Output);
234-
classifyModel(node, mutation.mutatedType);
243+
classifyModel(node);
235244
visitedModelOriginals.push(node);
236245
},
237246
enum: (node: Enum) => {

0 commit comments

Comments
 (0)