Skip to content

Commit 0e533af

Browse files
committed
refactor(ai): remove seenInputObjects
1 parent 3cbe6a6 commit 0e533af

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

packages/ai/src/mocking/GrowingSchema.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
FieldNode,
77
FormattedExecutionResult,
88
GraphQLCompositeType,
9+
GraphQLNamedType,
910
InputObjectTypeDefinitionNode,
1011
InputObjectTypeExtensionNode,
1112
InputValueDefinitionNode,
@@ -181,8 +182,6 @@ export class GrowingSchema {
181182
// the input objects are correct.
182183
private seenQueries = new WeakSet<GraphQLOperation>();
183184

184-
private seenInputObjects: Record<string, string[]> = {};
185-
186185
public validateQuery(query: DocumentNode) {
187186
const errors = validate(this.schema, query, enforcedRules);
188187
if (errors.length > 0) {
@@ -204,17 +203,6 @@ export class GrowingSchema {
204203

205204
this.validateResponseAgainstSchema(operation, response);
206205
this.seenQueries.add(operation);
207-
208-
// Track the fields of each input object so we can avoid
209-
// creating duplicate input objects.
210-
Object.entries(this.schema.getTypeMap()).forEach(([name, node]) => {
211-
if (node instanceof GraphQLInputObjectType) {
212-
this.seenInputObjects[name] =
213-
node.astNode?.fields?.map((field) => {
214-
return field.name.value;
215-
}) || [];
216-
}
217-
});
218206
} catch (e) {
219207
this.schema = previousSchema;
220208
throw e;
@@ -455,6 +443,10 @@ export class GrowingSchema {
455443
}
456444
}
457445

446+
private getType<T extends GraphQLNamedType>(name: string): T {
447+
return this.schema.getType(name) as T;
448+
}
449+
458450
private getVariableDefinitionsFromAncestors(
459451
ancestors: readonly (ASTNode | readonly ASTNode[])[]
460452
): OperationVariableDefinitions {
@@ -616,7 +608,7 @@ export class GrowingSchema {
616608
return [
617609
{
618610
kind:
619-
this.seenInputObjects[name] ?
611+
this.getType(name) ?
620612
Kind.INPUT_OBJECT_TYPE_EXTENSION
621613
: Kind.INPUT_OBJECT_TYPE_DEFINITION,
622614
name: { kind: Kind.NAME, value: name },
@@ -633,6 +625,11 @@ export class GrowingSchema {
633625
fields: InputValueDefinitionNode[];
634626
inputObjects: InputObjectsList;
635627
} {
628+
const existingInputObject =
629+
this.getType<GraphQLInputObjectType>(inputObjectName);
630+
const existingInputObjectFields =
631+
existingInputObject?.astNode?.fields?.map((field) => field.name.value) ||
632+
[];
636633
const inputObjects: InputObjectsList = [];
637634

638635
let valuesToHandle = valuesInScope;
@@ -727,8 +724,7 @@ export class GrowingSchema {
727724
} as InputValueDefinitionNode;
728725
})
729726
.filter(
730-
(field) =>
731-
!this.seenInputObjects[inputObjectName]?.includes(field.name.value)
727+
(field) => !existingInputObjectFields?.includes(field.name.value)
732728
);
733729
return { fields, inputObjects };
734730
}

0 commit comments

Comments
 (0)