Skip to content

Commit

Permalink
OverlappingFieldsCanBeMergedRule: simplify argument comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jan 13, 2022
1 parent 40c160e commit d1037a5
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,8 @@ function findConflict(
];
}

// FIXME https://github.com/graphql/graphql-js/issues/2203
const args1 = /* c8 ignore next */ node1.arguments ?? [];
const args2 = /* c8 ignore next */ node2.arguments ?? [];

// Two field calls must have the same arguments.
if (!sameArguments(args1, args2)) {
if (stringifyArguments(node1) !== stringifyArguments(node2)) {
return [
[responseName, 'they have differing arguments'],
[node1],
Expand Down Expand Up @@ -639,26 +635,19 @@ function findConflict(
}
}

function sameArguments(
arguments1: ReadonlyArray<ArgumentNode>,
arguments2: ReadonlyArray<ArgumentNode>,
): boolean {
if (arguments1.length !== arguments2.length) {
return false;
}
return arguments1.every((argument1) => {
const argument2 = arguments2.find(
(argument) => argument.name.value === argument1.name.value,
);
if (!argument2) {
return false;
}
return stringifyValue(argument1.value) === stringifyValue(argument2.value);
});
}

function stringifyValue(value: ValueNode): string {
return print(sortValueNode(value));
function stringifyArguments(fieldNode: FieldNode): string {
// FIXME https://github.com/graphql/graphql-js/issues/2203
const args = /* c8 ignore next */ fieldNode.arguments ?? [];

const inputObjectWithArgs = {
kind: Kind.OBJECT,
fields: args.map((argNode) => ({
kind: Kind.OBJECT_FIELD,
name: argNode.name,
value: argNode.value,
})),
};
return print(sortValueNode(inputObjectWithArgs));
}

// Two types conflict if both types could not apply to a value simultaneously.
Expand Down

0 comments on commit d1037a5

Please sign in to comment.