...
1 package validator
2
3 import (
4 "strconv"
5
6 "github.com/vektah/gqlparser/ast"
7 . "github.com/vektah/gqlparser/validator"
8 )
9
10 func init() {
11 AddRule("SingleFieldSubscriptions", func(observers *Events, addError AddErrFunc) {
12 observers.OnOperation(func(walker *Walker, operation *ast.OperationDefinition) {
13 if operation.Operation != ast.Subscription {
14 return
15 }
16
17 if len(operation.SelectionSet) > 1 {
18 name := "Anonymous Subscription"
19 if operation.Name != "" {
20 name = `Subscription ` + strconv.Quote(operation.Name)
21 }
22
23 addError(
24 Message(`%s must select only one top level field.`, name),
25 At(operation.SelectionSet[1].GetPosition()),
26 )
27 }
28 })
29 })
30 }
31
View as plain text