Skip to content

Commit 2991112

Browse files
committed
review changes
1 parent 70ff0ed commit 2991112

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ describe('Validate: Overlapping fields can be merged', () => {
179179
]);
180180
});
181181

182+
it('different stream directive extra argument', () => {
183+
expectErrors(`
184+
fragment conflictingArgs on Dog {
185+
name @stream(label: "streamLabel", initialCount: 1)
186+
name @stream(label: "streamLabel", initialCount: 1, extraArg: true)
187+
}
188+
`).toDeepEqual([
189+
{
190+
message:
191+
'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
192+
locations: [
193+
{ line: 3, column: 9 },
194+
{ line: 4, column: 9 },
195+
],
196+
},
197+
]);
198+
});
199+
182200
it('mix of stream and no stream', () => {
183201
expectErrors(`
184202
fragment conflictingArgs on Dog {

src/validation/rules/OverlappingFieldsCanBeMergedRule.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { Maybe } from '../../jsutils/Maybe.js';
44
import { GraphQLError } from '../../error/GraphQLError.js';
55

66
import type {
7-
ArgumentNode,
87
DirectiveNode,
98
FieldNode,
109
FragmentDefinitionNode,
@@ -593,7 +592,7 @@ function findConflict(
593592
}
594593

595594
// Two field calls must have the same arguments.
596-
if (!sameArguments(node1.arguments, node2.arguments)) {
595+
if (!sameArguments(node1, node2)) {
597596
return [
598597
[responseName, 'they have differing arguments'],
599598
[node1],
@@ -651,24 +650,36 @@ function findConflict(
651650
}
652651

653652
function sameArguments(
654-
arguments1: ReadonlyArray<ArgumentNode> = [],
655-
arguments2: ReadonlyArray<ArgumentNode> = [],
653+
node1: FieldNode | DirectiveNode,
654+
node2: FieldNode | DirectiveNode,
656655
): boolean {
657-
if (arguments1?.length !== arguments2?.length) {
656+
const args1 = node1.arguments;
657+
const args2 = node2.arguments;
658+
659+
if (args1 === undefined || args1.length === 0) {
660+
return args2 === undefined || args2.length === 0;
661+
}
662+
if (args2 === undefined || args2.length === 0) {
658663
return false;
659664
}
660-
return arguments1.every((argument1) => {
661-
const argument2 = arguments2.find(
662-
(argument) => argument.name.value === argument1.name.value,
663-
);
664-
if (!argument2) {
665+
666+
if (args1.length !== args2.length) {
667+
return false;
668+
}
669+
670+
const values2 = new Map(args2.map(({ name, value }) => [name.value, value]));
671+
return args1.every((arg1) => {
672+
const value1 = arg1.value;
673+
const value2 = values2.get(arg1.name.value);
674+
if (value2 === undefined) {
665675
return false;
666676
}
667-
return stringifyValue(argument1.value) === stringifyValue(argument2.value);
677+
678+
return stringifyValue(value1) === stringifyValue(value2);
668679
});
669680
}
670681

671-
function stringifyValue(value: ValueNode): string {
682+
function stringifyValue(value: ValueNode): string | null {
672683
return print(sortValueNode(value));
673684
}
674685

@@ -689,7 +700,7 @@ function sameStreams(
689700
return true;
690701
} else if (stream1 && stream2) {
691702
// check if both fields have equivalent streams
692-
return sameArguments(stream1.arguments, stream2.arguments);
703+
return sameArguments(stream1, stream2);
693704
}
694705
// fields have a mix of stream and no stream
695706
return false;

0 commit comments

Comments
 (0)