Skip to content

Commit a6ca40f

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

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

packages/ai/src/mocking/GrowingSchema.ts

Lines changed: 15 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,13 @@ export class GrowingSchema {
455443
}
456444
}
457445

446+
private getType<T extends GraphQLNamedType>(name: string): T | undefined {
447+
if (!name) {
448+
return undefined;
449+
}
450+
return this.schema.getType(name) as T;
451+
}
452+
458453
private getVariableDefinitionsFromAncestors(
459454
ancestors: readonly (ASTNode | readonly ASTNode[])[]
460455
): OperationVariableDefinitions {
@@ -616,7 +611,7 @@ export class GrowingSchema {
616611
return [
617612
{
618613
kind:
619-
this.seenInputObjects[name] ?
614+
this.getType(name) ?
620615
Kind.INPUT_OBJECT_TYPE_EXTENSION
621616
: Kind.INPUT_OBJECT_TYPE_DEFINITION,
622617
name: { kind: Kind.NAME, value: name },
@@ -633,6 +628,11 @@ export class GrowingSchema {
633628
fields: InputValueDefinitionNode[];
634629
inputObjects: InputObjectsList;
635630
} {
631+
const existingInputObject =
632+
this.getType<GraphQLInputObjectType>(inputObjectName);
633+
const existingInputObjectFields =
634+
existingInputObject?.astNode?.fields?.map((field) => field.name.value) ||
635+
[];
636636
const inputObjects: InputObjectsList = [];
637637

638638
let valuesToHandle = valuesInScope;
@@ -727,8 +727,7 @@ export class GrowingSchema {
727727
} as InputValueDefinitionNode;
728728
})
729729
.filter(
730-
(field) =>
731-
!this.seenInputObjects[inputObjectName]?.includes(field.name.value)
730+
(field) => !existingInputObjectFields?.includes(field.name.value)
732731
);
733732
return { fields, inputObjects };
734733
}

0 commit comments

Comments
 (0)