Complete GraphQL implementation in Go with schema definition, query execution, and validation.
Import: "github.com/graphql-go/graphql/gqlerrors"
A rich error type with location information, used internally during parsing, validation, and execution.
type Error struct {
Message string
Stack string
Nodes []ast.Node
Source *source.Source
Positions []int
Locations []location.SourceLocation
OriginalError error
Path []interface{}
}
func (g Error) Error() string // implements Go error interfaceA JSON-serializable error for inclusion in Result.Errors. Implements error.
type FormattedError struct {
Message string `json:"message"`
Locations []location.SourceLocation `json:"locations"`
Path []interface{} `json:"path,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
// unexported fields
}
func (g FormattedError) Error() string
func (g FormattedError) OriginalError() errortype FormattedErrors []FormattedError
func (errs FormattedErrors) Len() int
func (errs FormattedErrors) Less(i, j int) bool
func (errs FormattedErrors) Swap(i, j int)Implements sort.Interface for sorting.
type ExtendedError interface {
error
Extensions() map[string]interface{}
}If an error returned from a resolver implements ExtendedError, the extensions data is included in FormattedError.Extensions.
func NewError(
message string,
nodes []ast.Node,
stack string,
source *source.Source,
positions []int,
origError error,
) *Error
func NewErrorWithPath(
message string,
nodes []ast.Node,
stack string,
source *source.Source,
positions []int,
path []interface{},
origError error,
) *Error
func NewLocatedError(err interface{}, nodes []ast.Node) *Error
// Deprecated since 0.4.18: use graphql.NewLocatedError() instead.
func NewSyntaxError(s *source.Source, position int, description string) *Errorfunc FormatError(err error) FormattedError
func FormatErrors(errs ...error) []FormattedError
func NewFormattedError(message string) FormattedError
func FieldASTsToNodeASTs(fieldASTs []*ast.Field) []ast.Node// from "github.com/graphql-go/graphql"
func NewLocatedError(err interface{}, nodes []ast.Node) *gqlerrors.Error
func NewLocatedErrorWithPath(err interface{}, nodes []ast.Node, path []interface{}) *gqlerrors.ErrorUse these in resolvers to attach AST node location to errors.
// from "github.com/graphql-go/graphql/language/location"
type SourceLocation struct {
Line int `json:"line"`
Column int `json:"column"`
}
func GetLocation(s *source.Source, position int) SourceLocation// Resolver returning an error
resolve := func(p graphql.ResolveParams) (interface{}, error) {
val, err := fetchData(p.Args["id"].(string))
if err != nil {
return nil, err // appears in Result.Errors as FormattedError
}
return val, nil
}
// Resolver with extended error
type myError struct {
msg string
code string
}
func (e *myError) Error() string { return e.msg }
func (e *myError) Extensions() map[string]interface{} {
return map[string]interface{}{"code": e.code}
}
resolve := func(p graphql.ResolveParams) (interface{}, error) {
return nil, &myError{msg: "not found", code: "NOT_FOUND"}
// Extensions field in FormattedError will contain {"code": "NOT_FOUND"}
}Install with Tessl CLI
npx tessl i tessl/golang-github-com--graphql-go--graphql@0.8.0docs