/
rule_known_type_names.go
41 lines (33 loc) · 1.09 KB
/
rule_known_type_names.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package rules
import (
"github.com/bucketd/go-graphqlparser/ast"
"github.com/bucketd/go-graphqlparser/validation"
)
// KnownTypeNames ...
func KnownTypeNames(w *validation.Walker) {
w.AddNamedTypeEnterEventHandler(func(ctx *validation.Context, t ast.Type) {
typeName := t.NamedType
var existsInSchema bool
var existsInDocument bool
_, existsInSchema = ctx.Schema.Types[typeName]
// If we're validating an SDL document, types declared in the current document must also be
// taken into account.
if ctx.SDLContext != nil {
_, existsInDocument = ctx.SDLContext.TypeDefinitions[typeName]
}
if !existsInSchema && !existsInDocument && !isSpecifiedScalarName(typeName) {
ctx.AddError(validation.UnknownTypeError(typeName, 0, 0))
}
})
}
// isSpecifiedScalarName ...
//
// NOTE: This function is needed for when a schema is being parsed, and we aren't extending a
// schema. At this point we haven't "registered" the built-in scalar types.
func isSpecifiedScalarName(typeName string) bool {
switch typeName {
case "String", "Int", "Float", "Boolean", "ID":
return true
}
return false
}