Skip to content

Commit 21fd3b8

Browse files
committed
feat: create a new ScalarLeafsRuleWithoutSuggestions validator rule
ScalarLeafsRule also appends "Did you mean" schema suggestions like e.g., KnownTypeNamesRule, but wasn't treated previously like other validator rules, possibly because it was calling `Suggestf()` which adds the "Did you mean" string.
1 parent 48a3d5e commit 21fd3b8

1 file changed

Lines changed: 35 additions & 21 deletions

File tree

validator/rules/scalar_leafs.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ import (
66
. "github.com/vektah/gqlparser/v2/validator/core"
77
)
88

9-
var ScalarLeafsRule = Rule{
10-
Name: "ScalarLeafs",
11-
RuleFunc: func(observers *Events, addError AddErrFunc) {
12-
observers.OnField(func(walker *Walker, field *ast.Field) {
13-
if field.Definition == nil {
14-
return
15-
}
9+
func ruleFuncScalarLeafs(observers *Events, addError AddErrFunc, disableSuggestion bool) {
10+
observers.OnField(func(walker *Walker, field *ast.Field) {
11+
if field.Definition == nil {
12+
return
13+
}
1614

17-
fieldType := walker.Schema.Types[field.Definition.Type.Name()]
18-
if fieldType == nil {
19-
return
20-
}
15+
fieldType := walker.Schema.Types[field.Definition.Type.Name()]
16+
if fieldType == nil {
17+
return
18+
}
2119

22-
if fieldType.IsLeafType() && len(field.SelectionSet) > 0 {
20+
if fieldType.IsLeafType() && len(field.SelectionSet) > 0 {
21+
addError(
22+
Message(`Field "%s" must not have a selection since type "%s" has no subfields.`, field.Name, fieldType.Name),
23+
At(field.Position),
24+
)
25+
}
26+
27+
if !fieldType.IsLeafType() && len(field.SelectionSet) == 0 {
28+
if disableSuggestion {
2329
addError(
24-
Message(
25-
`Field "%s" must not have a selection since type "%s" has no subfields.`,
26-
field.Name,
27-
fieldType.Name,
28-
),
30+
Message(`Field "%s" of type "%s" must have a selection of subfields.`, field.Name, field.Definition.Type.String()),
2931
At(field.Position),
3032
)
31-
}
32-
33-
if !fieldType.IsLeafType() && len(field.SelectionSet) == 0 {
33+
} else {
3434
addError(
3535
Message(
3636
`Field "%s" of type "%s" must have a selection of subfields.`,
@@ -41,6 +41,20 @@ var ScalarLeafsRule = Rule{
4141
At(field.Position),
4242
)
4343
}
44-
})
44+
}
45+
})
46+
}
47+
48+
var ScalarLeafsRule = Rule{
49+
Name: "ScalarLeafs",
50+
RuleFunc: func(observers *Events, addError AddErrFunc) {
51+
ruleFuncScalarLeafs(observers, addError, false)
52+
},
53+
}
54+
55+
var ScalarLeafsRuleWithoutSuggestions = Rule{
56+
Name: "ScalarLeafsWithoutSuggestions",
57+
RuleFunc: func(observers *Events, addError AddErrFunc) {
58+
ruleFuncScalarLeafs(observers, addError, true)
4559
},
4660
}

0 commit comments

Comments
 (0)