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 17, 2022
1 parent 730d5af commit efdbbcf
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Expand Up @@ -5,11 +5,10 @@ import type { ObjMap } from '../../jsutils/ObjMap';
import { GraphQLError } from '../../error/GraphQLError';

import type {
ArgumentNode,
FieldNode,
FragmentDefinitionNode,
ObjectValueNode,
SelectionSetNode,
ValueNode,
} from '../../language/ast';
import { Kind } from '../../language/kinds';
import { print } from '../../language/printer';
Expand Down Expand Up @@ -588,12 +587,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 +634,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: ObjectValueNode = {
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 efdbbcf

Please sign in to comment.