Skip to content

Commit 4afb263

Browse files
committed
Validation: improving overlapping fields quality (#386)
This improves the overlapping fields validation performance and improves error reporting quality by separating the concepts of checking fields "within" a single collection of fields from checking fields "between" two different collections of fields. This ensures for deeply overlapping fields that nested fields are not checked against each other repeatedly. Extending this concept further, fragment spreads are no longer expanded inline before looking for conflicts, instead the fields within a fragment are compared to the fields with the selection set which contained the referencing fragment spread. e.g. ```graphql { same: a same: b ...X } fragment X on T { same: c same: d } ``` In the above example, the initial query body is checked "within" so `a` is compared to `b`. Also, the fragment `X` is checked "within" so `c` is compared to `d`. Because of the fragment spread, the query body and fragment `X` are checked "between" so that `a` and `b` are each compared to `c` and `d`. In this trivial example, no fewer checks are performed, but in the case where fragments are referenced multiple times, this reduces the overall number of checks (regardless of memoization). **BREAKING**: This can change the order of fields reported when a conflict arises when fragment spreads are involved. If you are checking the precise output of errors (e.g. for unit tests), you may find existing errors change from `"a" and "c" are different fields` to `"c" and "a" are different fields`. From a perf point of view, this is fairly minor as the memoization "PairSet" was already keeping these repeated checks from consuming time, however this will reduce the number of memoized hits because of the algorithm improvement. From an error reporting point of view, this reports nearest-common-ancestor issues when found in a fragment that comes later in the validation process. I've added a test which fails with the existing impl and now passes, as well as changed a comment. This also fixes an error where validation issues could be missed because of an over-eager memoization. I've also modified the `PairSet` to be aware of both forms of memoization, also represented by a previously failing test.
1 parent 688a1ee commit 4afb263

File tree

2 files changed

+677
-157
lines changed

2 files changed

+677
-157
lines changed

src/validation/__tests__/OverlappingFieldsCanBeMerged-test.js

+157-8
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ describe('Validate: Overlapping fields can be merged', () => {
247247
`, [
248248
{ message: fieldsConflictMessage('x', 'a and b are different fields'),
249249
locations: [ { line: 18, column: 9 }, { line: 21, column: 9 } ] },
250-
{ message: fieldsConflictMessage('x', 'a and c are different fields'),
251-
locations: [ { line: 18, column: 9 }, { line: 14, column: 11 } ] },
252-
{ message: fieldsConflictMessage('x', 'b and c are different fields'),
253-
locations: [ { line: 21, column: 9 }, { line: 14, column: 11 } ] }
250+
{ message: fieldsConflictMessage('x', 'c and a are different fields'),
251+
locations: [ { line: 14, column: 11 }, { line: 18, column: 9 } ] },
252+
{ message: fieldsConflictMessage('x', 'c and b are different fields'),
253+
locations: [ { line: 14, column: 11 }, { line: 21, column: 9 } ] }
254254
]);
255255
});
256256

@@ -363,6 +363,97 @@ describe('Validate: Overlapping fields can be merged', () => {
363363
]);
364364
});
365365

366+
it('reports deep conflict to nearest common ancestor in fragments', () => {
367+
expectFailsRule(OverlappingFieldsCanBeMerged, `
368+
{
369+
field {
370+
...F
371+
}
372+
field {
373+
...F
374+
}
375+
}
376+
fragment F on T {
377+
deepField {
378+
deeperField {
379+
x: a
380+
}
381+
deeperField {
382+
x: b
383+
}
384+
},
385+
deepField {
386+
deeperField {
387+
y
388+
}
389+
}
390+
}
391+
`, [
392+
{ message: fieldsConflictMessage(
393+
'deeperField', [ [ 'x', 'a and b are different fields' ] ]
394+
),
395+
locations: [
396+
{ line: 12, column: 11 },
397+
{ line: 13, column: 13 },
398+
{ line: 15, column: 11 },
399+
{ line: 16, column: 13 } ] },
400+
]);
401+
});
402+
403+
it('reports deep conflict in nested fragments', () => {
404+
expectFailsRule(OverlappingFieldsCanBeMerged, `
405+
{
406+
field {
407+
...F
408+
}
409+
field {
410+
...I
411+
}
412+
}
413+
fragment F on T {
414+
x: a
415+
...G
416+
}
417+
fragment G on T {
418+
y: c
419+
}
420+
fragment I on T {
421+
y: d
422+
...J
423+
}
424+
fragment J on T {
425+
x: b
426+
}
427+
`, [
428+
{ message: fieldsConflictMessage(
429+
'field', [ [ 'x', 'a and b are different fields' ],
430+
[ 'y', 'c and d are different fields' ] ]
431+
),
432+
locations: [
433+
{ line: 3, column: 9 },
434+
{ line: 11, column: 9 },
435+
{ line: 15, column: 9 },
436+
{ line: 6, column: 9 },
437+
{ line: 22, column: 9 },
438+
{ line: 18, column: 9 } ] },
439+
]);
440+
});
441+
442+
it('ignores unknown fragments', () => {
443+
expectPassesRule(OverlappingFieldsCanBeMerged, `
444+
{
445+
field
446+
...Unknown
447+
...Known
448+
}
449+
450+
fragment Known on T {
451+
field
452+
...OtherUnknown
453+
}
454+
`);
455+
});
456+
366457
describe('return types must be unambiguous', () => {
367458

368459
const SomeBox = new GraphQLInterfaceType({
@@ -537,6 +628,64 @@ describe('Validate: Overlapping fields can be merged', () => {
537628
]);
538629
});
539630

631+
it('reports correctly when a non-exclusive follows an exclusive', () => {
632+
expectFailsRuleWithSchema(schema, OverlappingFieldsCanBeMerged, `
633+
{
634+
someBox {
635+
... on IntBox {
636+
deepBox {
637+
...X
638+
}
639+
}
640+
}
641+
someBox {
642+
... on StringBox {
643+
deepBox {
644+
...Y
645+
}
646+
}
647+
}
648+
memoed: someBox {
649+
... on IntBox {
650+
deepBox {
651+
...X
652+
}
653+
}
654+
}
655+
memoed: someBox {
656+
... on StringBox {
657+
deepBox {
658+
...Y
659+
}
660+
}
661+
}
662+
other: someBox {
663+
...X
664+
}
665+
other: someBox {
666+
...Y
667+
}
668+
}
669+
fragment X on SomeBox {
670+
scalar
671+
}
672+
fragment Y on SomeBox {
673+
scalar: unrelatedField
674+
}
675+
`, [
676+
{ message: fieldsConflictMessage(
677+
'other',
678+
[ [ 'scalar', 'scalar and unrelatedField are different fields' ] ]
679+
),
680+
locations: [
681+
{ line: 31, column: 11 },
682+
{ line: 39, column: 11 },
683+
{ line: 34, column: 11 },
684+
{ line: 42, column: 11 },
685+
] }
686+
]);
687+
});
688+
540689
it('disallows differing return type nullability despite no overlap', () => {
541690
expectFailsRuleWithSchema(schema, OverlappingFieldsCanBeMerged, `
542691
{
@@ -725,15 +874,15 @@ describe('Validate: Overlapping fields can be merged', () => {
725874
`, [
726875
{ message: fieldsConflictMessage(
727876
'edges',
728-
[ [ 'node', [ [ 'id', 'id and name are different fields' ] ] ] ]
877+
[ [ 'node', [ [ 'id', 'name and id are different fields' ] ] ] ]
729878
),
730879
locations: [
731-
{ line: 14, column: 11 },
732-
{ line: 15, column: 13 },
733-
{ line: 16, column: 15 },
734880
{ line: 5, column: 13 },
735881
{ line: 6, column: 15 },
736882
{ line: 7, column: 17 },
883+
{ line: 14, column: 11 },
884+
{ line: 15, column: 13 },
885+
{ line: 16, column: 15 },
737886
] }
738887
]);
739888
});

0 commit comments

Comments
 (0)