Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OverlappingFieldsCanBeMergedRule: simplify argument comparison #3457

Merged
merged 1 commit into from Jan 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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