...
1 package validator
2
3 import (
4 "fmt"
5
6 "github.com/vektah/gqlparser/v2/ast"
7 "github.com/vektah/gqlparser/v2/gqlerror"
8 )
9
10 type ErrorOption func(err *gqlerror.Error)
11
12 func Message(msg string, args ...interface{}) ErrorOption {
13 return func(err *gqlerror.Error) {
14 err.Message += fmt.Sprintf(msg, args...)
15 }
16 }
17
18 func At(position *ast.Position) ErrorOption {
19 return func(err *gqlerror.Error) {
20 if position == nil {
21 return
22 }
23 err.Locations = append(err.Locations, gqlerror.Location{
24 Line: position.Line,
25 Column: position.Column,
26 })
27 if position.Src.Name != "" {
28 err.SetFile(position.Src.Name)
29 }
30 }
31 }
32
33 func SuggestListQuoted(prefix string, typed string, suggestions []string) ErrorOption {
34 suggested := SuggestionList(typed, suggestions)
35 return func(err *gqlerror.Error) {
36 if len(suggested) > 0 {
37 err.Message += " " + prefix + " " + QuotedOrList(suggested...) + "?"
38 }
39 }
40 }
41
42 func SuggestListUnquoted(prefix string, typed string, suggestions []string) ErrorOption {
43 suggested := SuggestionList(typed, suggestions)
44 return func(err *gqlerror.Error) {
45 if len(suggested) > 0 {
46 err.Message += " " + prefix + " " + OrList(suggested...) + "?"
47 }
48 }
49 }
50
51 func Suggestf(suggestion string, args ...interface{}) ErrorOption {
52 return func(err *gqlerror.Error) {
53 err.Message += " Did you mean " + fmt.Sprintf(suggestion, args...) + "?"
54 }
55 }
56
View as plain text