Skip to content

Commit d54ea4d

Browse files
sogkochris-ramon
authored andcommitted
Improve validation error message when field names conflict
graphql/graphql-js#363 * Improve validation error message when field names conflict * Remove extra hint and add unit test
1 parent c3d5b0d commit d54ea4d

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed

rules.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,8 +1510,8 @@ func OverlappingFieldsCanBeMergedRule(context *ValidationContext) *ValidationRul
15101510
reason := c.Reason
15111511
reportError(
15121512
context,
1513-
fmt.Sprintf(
1514-
`Fields "%v" conflict because %v.`,
1513+
fmt.Sprintf(`Fields "%v" conflict because %v. `+
1514+
`Use different aliases on the fields to fetch both if this was intentional.`,
15151515
responseName,
15161516
reasonMessage(reason),
15171517
),

rules_overlapping_fields_can_be_merged_test.go

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_SameAliasesWithDifferentFieldTarg
7474
fido: nickname
7575
}
7676
`, []gqlerrors.FormattedError{
77-
testutil.RuleError(`Fields "fido" conflict because name and nickname are different fields.`, 3, 9, 4, 9),
77+
testutil.RuleError(`Fields "fido" conflict because name and nickname are different fields. `+
78+
`Use different aliases on the fields to fetch both if this was intentional.`,
79+
3, 9, 4, 9),
7880
})
7981
}
8082
func TestValidate_OverlappingFieldsCanBeMerged_SameAliasesAllowedOnNonOverlappingFields(t *testing.T) {
@@ -96,7 +98,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_AliasMaskingDirectFieldAccess(t *
9698
name
9799
}
98100
`, []gqlerrors.FormattedError{
99-
testutil.RuleError(`Fields "name" conflict because nickname and name are different fields.`, 3, 9, 4, 9),
101+
testutil.RuleError(`Fields "name" conflict because nickname and name are different fields. `+
102+
`Use different aliases on the fields to fetch both if this was intentional.`,
103+
3, 9, 4, 9),
100104
})
101105
}
102106
func TestValidate_OverlappingFieldsCanBeMerged_DifferentArgs_SecondAddsAnArgument(t *testing.T) {
@@ -106,7 +110,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_DifferentArgs_SecondAddsAnArgumen
106110
doesKnowCommand(dogCommand: HEEL)
107111
}
108112
`, []gqlerrors.FormattedError{
109-
testutil.RuleError(`Fields "doesKnowCommand" conflict because they have differing arguments.`, 3, 9, 4, 9),
113+
testutil.RuleError(`Fields "doesKnowCommand" conflict because they have differing arguments. `+
114+
`Use different aliases on the fields to fetch both if this was intentional.`,
115+
3, 9, 4, 9),
110116
})
111117
}
112118
func TestValidate_OverlappingFieldsCanBeMerged_DifferentArgs_SecondMissingAnArgument(t *testing.T) {
@@ -116,7 +122,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_DifferentArgs_SecondMissingAnArgu
116122
doesKnowCommand
117123
}
118124
`, []gqlerrors.FormattedError{
119-
testutil.RuleError(`Fields "doesKnowCommand" conflict because they have differing arguments.`, 3, 9, 4, 9),
125+
testutil.RuleError(`Fields "doesKnowCommand" conflict because they have differing arguments. `+
126+
`Use different aliases on the fields to fetch both if this was intentional.`,
127+
3, 9, 4, 9),
120128
})
121129
}
122130
func TestValidate_OverlappingFieldsCanBeMerged_ConflictingArgs(t *testing.T) {
@@ -126,7 +134,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_ConflictingArgs(t *testing.T) {
126134
doesKnowCommand(dogCommand: HEEL)
127135
}
128136
`, []gqlerrors.FormattedError{
129-
testutil.RuleError(`Fields "doesKnowCommand" conflict because they have differing arguments.`, 3, 9, 4, 9),
137+
testutil.RuleError(`Fields "doesKnowCommand" conflict because they have differing arguments. `+
138+
`Use different aliases on the fields to fetch both if this was intentional.`,
139+
3, 9, 4, 9),
130140
})
131141
}
132142
func TestValidate_OverlappingFieldsCanBeMerged_AllowDifferentArgsWhereNoConflictIsPossible(t *testing.T) {
@@ -156,7 +166,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_EncountersConflictInFragments(t *
156166
x: b
157167
}
158168
`, []gqlerrors.FormattedError{
159-
testutil.RuleError(`Fields "x" conflict because a and b are different fields.`, 7, 9, 10, 9),
169+
testutil.RuleError(`Fields "x" conflict because a and b are different fields. `+
170+
`Use different aliases on the fields to fetch both if this was intentional.`,
171+
7, 9, 10, 9),
160172
})
161173
}
162174
func TestValidate_OverlappingFieldsCanBeMerged_ReportsEachConflictOnce(t *testing.T) {
@@ -183,9 +195,15 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReportsEachConflictOnce(t *testin
183195
x: b
184196
}
185197
`, []gqlerrors.FormattedError{
186-
testutil.RuleError(`Fields "x" conflict because a and b are different fields.`, 18, 9, 21, 9),
187-
testutil.RuleError(`Fields "x" conflict because a and c are different fields.`, 18, 9, 14, 11),
188-
testutil.RuleError(`Fields "x" conflict because b and c are different fields.`, 21, 9, 14, 11),
198+
testutil.RuleError(`Fields "x" conflict because a and b are different fields. `+
199+
`Use different aliases on the fields to fetch both if this was intentional.`,
200+
18, 9, 21, 9),
201+
testutil.RuleError(`Fields "x" conflict because a and c are different fields. `+
202+
`Use different aliases on the fields to fetch both if this was intentional.`,
203+
18, 9, 14, 11),
204+
testutil.RuleError(`Fields "x" conflict because b and c are different fields. `+
205+
`Use different aliases on the fields to fetch both if this was intentional.`,
206+
21, 9, 14, 11),
189207
})
190208
}
191209
func TestValidate_OverlappingFieldsCanBeMerged_DeepConflict(t *testing.T) {
@@ -199,7 +217,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_DeepConflict(t *testing.T) {
199217
}
200218
}
201219
`, []gqlerrors.FormattedError{
202-
testutil.RuleError(`Fields "field" conflict because subfields "x" conflict because a and b are different fields.`,
220+
testutil.RuleError(`Fields "field" conflict because subfields "x" conflict because a and b are different fields. `+
221+
`Use different aliases on the fields to fetch both if this was intentional.`,
203222
3, 9,
204223
4, 11,
205224
6, 9,
@@ -219,9 +238,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_DeepConflictWithMultipleIssues(t
219238
}
220239
}
221240
`, []gqlerrors.FormattedError{
222-
testutil.RuleError(
223-
`Fields "field" conflict because subfields "x" conflict because a and b are different fields and `+
224-
`subfields "y" conflict because c and d are different fields.`,
241+
testutil.RuleError(`Fields "field" conflict because subfields "x" conflict because a and b are different fields and `+
242+
`subfields "y" conflict because c and d are different fields. `+
243+
`Use different aliases on the fields to fetch both if this was intentional.`,
225244
3, 9,
226245
4, 11,
227246
5, 11,
@@ -245,9 +264,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_VeryDeepConflict(t *testing.T) {
245264
}
246265
}
247266
`, []gqlerrors.FormattedError{
248-
testutil.RuleError(
249-
`Fields "field" conflict because subfields "deepField" conflict because subfields "x" conflict because `+
250-
`a and b are different fields.`,
267+
testutil.RuleError(`Fields "field" conflict because subfields "deepField" conflict because subfields "x" conflict because `+
268+
`a and b are different fields. `+
269+
`Use different aliases on the fields to fetch both if this was intentional.`,
251270
3, 9,
252271
4, 11,
253272
5, 13,
@@ -274,9 +293,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReportsDeepConflictToNearestCommo
274293
}
275294
}
276295
`, []gqlerrors.FormattedError{
277-
testutil.RuleError(
278-
`Fields "deepField" conflict because subfields "x" conflict because `+
279-
`a and b are different fields.`,
296+
testutil.RuleError(`Fields "deepField" conflict because subfields "x" conflict because `+
297+
`a and b are different fields. `+
298+
`Use different aliases on the fields to fetch both if this was intentional.`,
280299
4, 11,
281300
5, 13,
282301
7, 11,
@@ -486,8 +505,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Conf
486505
}
487506
}
488507
`, []gqlerrors.FormattedError{
489-
testutil.RuleError(
490-
`Fields "scalar" conflict because they return conflicting types Int and String!.`,
508+
testutil.RuleError(`Fields "scalar" conflict because they return conflicting types Int and String!. `+
509+
`Use different aliases on the fields to fetch both if this was intentional.`,
491510
5, 15,
492511
8, 15),
493512
})
@@ -526,8 +545,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Disa
526545
}
527546
}
528547
`, []gqlerrors.FormattedError{
529-
testutil.RuleError(
530-
`Fields "scalar" conflict because they return conflicting types Int and String.`,
548+
testutil.RuleError(`Fields "scalar" conflict because they return conflicting types Int and String. `+
549+
`Use different aliases on the fields to fetch both if this was intentional.`,
531550
5, 15,
532551
8, 15),
533552
})
@@ -545,8 +564,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Disa
545564
}
546565
}
547566
`, []gqlerrors.FormattedError{
548-
testutil.RuleError(
549-
`Fields "scalar" conflict because they return conflicting types String! and String.`,
567+
testutil.RuleError(`Fields "scalar" conflict because they return conflicting types String! and String. `+
568+
`Use different aliases on the fields to fetch both if this was intentional.`,
550569
5, 15,
551570
8, 15),
552571
})
@@ -568,8 +587,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Disa
568587
}
569588
}
570589
`, []gqlerrors.FormattedError{
571-
testutil.RuleError(
572-
`Fields "box" conflict because they return conflicting types [StringBox] and StringBox.`,
590+
testutil.RuleError(`Fields "box" conflict because they return conflicting types [StringBox] and StringBox. `+
591+
`Use different aliases on the fields to fetch both if this was intentional.`,
573592
5, 15,
574593
10, 15),
575594
})
@@ -590,8 +609,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Disa
590609
}
591610
}
592611
`, []gqlerrors.FormattedError{
593-
testutil.RuleError(
594-
`Fields "box" conflict because they return conflicting types StringBox and [StringBox].`,
612+
testutil.RuleError(`Fields "box" conflict because they return conflicting types StringBox and [StringBox]. `+
613+
`Use different aliases on the fields to fetch both if this was intentional.`,
595614
5, 15,
596615
10, 15),
597616
})
@@ -614,8 +633,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Disa
614633
}
615634
}
616635
`, []gqlerrors.FormattedError{
617-
testutil.RuleError(
618-
`Fields "val" conflict because scalar and unrelatedField are different fields.`,
636+
testutil.RuleError(`Fields "val" conflict because scalar and unrelatedField are different fields. `+
637+
`Use different aliases on the fields to fetch both if this was intentional.`,
619638
6, 17,
620639
7, 17),
621640
})
@@ -637,8 +656,8 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Disa
637656
}
638657
}
639658
`, []gqlerrors.FormattedError{
640-
testutil.RuleError(
641-
`Fields "box" conflict because subfields "scalar" conflict because they return conflicting types String and Int.`,
659+
testutil.RuleError(`Fields "box" conflict because subfields "scalar" conflict because they return conflicting types String and Int. `+
660+
`Use different aliases on the fields to fetch both if this was intentional.`,
642661
5, 15,
643662
6, 17,
644663
10, 15,
@@ -704,9 +723,9 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Comp
704723
}
705724
}
706725
`, []gqlerrors.FormattedError{
707-
testutil.RuleError(
708-
`Fields "edges" conflict because subfields "node" conflict because subfields "id" conflict because `+
709-
`id and name are different fields.`,
726+
testutil.RuleError(`Fields "edges" conflict because subfields "node" conflict because subfields "id" conflict because `+
727+
`id and name are different fields. `+
728+
`Use different aliases on the fields to fetch both if this was intentional.`,
710729
14, 11,
711730
15, 13,
712731
16, 15,

0 commit comments

Comments
 (0)