From a2a50f3de0c428c5a61e6a449191be3c4ded86ac Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 19 Jul 2018 14:15:50 +0200 Subject: webui: add a primitive graphql handler --- .../graphql-go/graphql/gqlerrors/error.go | 68 +++++++++++++++++++++ .../graphql-go/graphql/gqlerrors/formatted.go | 51 ++++++++++++++++ .../graphql-go/graphql/gqlerrors/located.go | 39 ++++++++++++ .../graphql-go/graphql/gqlerrors/sortutil.go | 30 ++++++++++ .../graphql-go/graphql/gqlerrors/syntax.go | 69 ++++++++++++++++++++++ 5 files changed, 257 insertions(+) create mode 100644 vendor/github.com/graphql-go/graphql/gqlerrors/error.go create mode 100644 vendor/github.com/graphql-go/graphql/gqlerrors/formatted.go create mode 100644 vendor/github.com/graphql-go/graphql/gqlerrors/located.go create mode 100644 vendor/github.com/graphql-go/graphql/gqlerrors/sortutil.go create mode 100644 vendor/github.com/graphql-go/graphql/gqlerrors/syntax.go (limited to 'vendor/github.com/graphql-go/graphql/gqlerrors') diff --git a/vendor/github.com/graphql-go/graphql/gqlerrors/error.go b/vendor/github.com/graphql-go/graphql/gqlerrors/error.go new file mode 100644 index 00000000..1b289bbb --- /dev/null +++ b/vendor/github.com/graphql-go/graphql/gqlerrors/error.go @@ -0,0 +1,68 @@ +package gqlerrors + +import ( + "fmt" + "reflect" + + "github.com/graphql-go/graphql/language/ast" + "github.com/graphql-go/graphql/language/location" + "github.com/graphql-go/graphql/language/source" +) + +type Error struct { + Message string + Stack string + Nodes []ast.Node + Source *source.Source + Positions []int + Locations []location.SourceLocation + OriginalError error +} + +// implements Golang's built-in `error` interface +func (g Error) Error() string { + return fmt.Sprintf("%v", g.Message) +} + +func NewError(message string, nodes []ast.Node, stack string, source *source.Source, positions []int, origError error) *Error { + if stack == "" && message != "" { + stack = message + } + if source == nil { + for _, node := range nodes { + // get source from first node + if node == nil || reflect.ValueOf(node).IsNil() { + continue + } + if node.GetLoc() != nil { + source = node.GetLoc().Source + } + break + } + } + if len(positions) == 0 && len(nodes) > 0 { + for _, node := range nodes { + if node == nil || reflect.ValueOf(node).IsNil() { + continue + } + if node.GetLoc() == nil { + continue + } + positions = append(positions, node.GetLoc().Start) + } + } + locations := []location.SourceLocation{} + for _, pos := range positions { + loc := location.GetLocation(source, pos) + locations = append(locations, loc) + } + return &Error{ + Message: message, + Stack: stack, + Nodes: nodes, + Source: source, + Positions: positions, + Locations: locations, + OriginalError: origError, + } +} diff --git a/vendor/github.com/graphql-go/graphql/gqlerrors/formatted.go b/vendor/github.com/graphql-go/graphql/gqlerrors/formatted.go new file mode 100644 index 00000000..3a1f8853 --- /dev/null +++ b/vendor/github.com/graphql-go/graphql/gqlerrors/formatted.go @@ -0,0 +1,51 @@ +package gqlerrors + +import ( + "errors" + + "github.com/graphql-go/graphql/language/location" +) + +type FormattedError struct { + Message string `json:"message"` + Locations []location.SourceLocation `json:"locations"` +} + +func (g FormattedError) Error() string { + return g.Message +} + +func NewFormattedError(message string) FormattedError { + err := errors.New(message) + return FormatError(err) +} + +func FormatError(err error) FormattedError { + switch err := err.(type) { + case FormattedError: + return err + case *Error: + return FormattedError{ + Message: err.Error(), + Locations: err.Locations, + } + case Error: + return FormattedError{ + Message: err.Error(), + Locations: err.Locations, + } + default: + return FormattedError{ + Message: err.Error(), + Locations: []location.SourceLocation{}, + } + } +} + +func FormatErrors(errs ...error) []FormattedError { + formattedErrors := []FormattedError{} + for _, err := range errs { + formattedErrors = append(formattedErrors, FormatError(err)) + } + return formattedErrors +} diff --git a/vendor/github.com/graphql-go/graphql/gqlerrors/located.go b/vendor/github.com/graphql-go/graphql/gqlerrors/located.go new file mode 100644 index 00000000..b02fcd8a --- /dev/null +++ b/vendor/github.com/graphql-go/graphql/gqlerrors/located.go @@ -0,0 +1,39 @@ +package gqlerrors + +import ( + "errors" + "github.com/graphql-go/graphql/language/ast" +) + +// NewLocatedError creates a graphql.Error with location info +// @deprecated 0.4.18 +// Already exists in `graphql.NewLocatedError()` +func NewLocatedError(err interface{}, nodes []ast.Node) *Error { + var origError error + message := "An unknown error occurred." + if err, ok := err.(error); ok { + message = err.Error() + origError = err + } + if err, ok := err.(string); ok { + message = err + origError = errors.New(err) + } + stack := message + return NewError( + message, + nodes, + stack, + nil, + []int{}, + origError, + ) +} + +func FieldASTsToNodeASTs(fieldASTs []*ast.Field) []ast.Node { + nodes := []ast.Node{} + for _, fieldAST := range fieldASTs { + nodes = append(nodes, fieldAST) + } + return nodes +} diff --git a/vendor/github.com/graphql-go/graphql/gqlerrors/sortutil.go b/vendor/github.com/graphql-go/graphql/gqlerrors/sortutil.go new file mode 100644 index 00000000..300a9407 --- /dev/null +++ b/vendor/github.com/graphql-go/graphql/gqlerrors/sortutil.go @@ -0,0 +1,30 @@ +package gqlerrors + +import "bytes" + +type FormattedErrors []FormattedError + +func (errs FormattedErrors) Len() int { + return len(errs) +} + +func (errs FormattedErrors) Swap(i, j int) { + errs[i], errs[j] = errs[j], errs[i] +} + +func (errs FormattedErrors) Less(i, j int) bool { + mCompare := bytes.Compare([]byte(errs[i].Message), []byte(errs[j].Message)) + lesserLine := errs[i].Locations[0].Line < errs[j].Locations[0].Line + eqLine := errs[i].Locations[0].Line == errs[j].Locations[0].Line + lesserColumn := errs[i].Locations[0].Column < errs[j].Locations[0].Column + if mCompare < 0 { + return true + } + if mCompare == 0 && lesserLine { + return true + } + if mCompare == 0 && eqLine && lesserColumn { + return true + } + return false +} diff --git a/vendor/github.com/graphql-go/graphql/gqlerrors/syntax.go b/vendor/github.com/graphql-go/graphql/gqlerrors/syntax.go new file mode 100644 index 00000000..abad6ade --- /dev/null +++ b/vendor/github.com/graphql-go/graphql/gqlerrors/syntax.go @@ -0,0 +1,69 @@ +package gqlerrors + +import ( + "fmt" + "regexp" + "strings" + + "github.com/graphql-go/graphql/language/ast" + "github.com/graphql-go/graphql/language/location" + "github.com/graphql-go/graphql/language/source" +) + +func NewSyntaxError(s *source.Source, position int, description string) *Error { + l := location.GetLocation(s, position) + return NewError( + fmt.Sprintf("Syntax Error %s (%d:%d) %s\n\n%s", s.Name, l.Line, l.Column, description, highlightSourceAtLocation(s, l)), + []ast.Node{}, + "", + s, + []int{position}, + nil, + ) +} + +// printCharCode here is slightly different from lexer.printCharCode() +func printCharCode(code rune) string { + // print as ASCII for printable range + if code >= 0x0020 { + return fmt.Sprintf(`%c`, code) + } + // Otherwise print the escaped form. e.g. `"\\u0007"` + return fmt.Sprintf(`\u%04X`, code) +} +func printLine(str string) string { + strSlice := []string{} + for _, runeValue := range str { + strSlice = append(strSlice, printCharCode(runeValue)) + } + return fmt.Sprintf(`%s`, strings.Join(strSlice, "")) +} +func highlightSourceAtLocation(s *source.Source, l location.SourceLocation) string { + line := l.Line + prevLineNum := fmt.Sprintf("%d", (line - 1)) + lineNum := fmt.Sprintf("%d", line) + nextLineNum := fmt.Sprintf("%d", (line + 1)) + padLen := len(nextLineNum) + lines := regexp.MustCompile("\r\n|[\n\r]").Split(string(s.Body), -1) + var highlight string + if line >= 2 { + highlight += fmt.Sprintf("%s: %s\n", lpad(padLen, prevLineNum), printLine(lines[line-2])) + } + highlight += fmt.Sprintf("%s: %s\n", lpad(padLen, lineNum), printLine(lines[line-1])) + for i := 1; i < (2 + padLen + l.Column); i++ { + highlight += " " + } + highlight += "^\n" + if line < len(lines) { + highlight += fmt.Sprintf("%s: %s\n", lpad(padLen, nextLineNum), printLine(lines[line])) + } + return highlight +} + +func lpad(l int, s string) string { + var r string + for i := 1; i < (l - len(s) + 1); i++ { + r += " " + } + return r + s +} -- cgit