Skip to content

Commit

Permalink
Merge pull request #4 from tribunadigital/update-upstream-v1.1.0
Browse files Browse the repository at this point in the history
Update upstream v1.1.0
  • Loading branch information
blssedvictim authored Feb 5, 2024
2 parents 00bd7bb + 542c043 commit fd2970f
Show file tree
Hide file tree
Showing 47 changed files with 1,816 additions and 1,148 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.idea
/.vscode
/internal/validation/testdata/graphql-js
/internal/validation/testdata/node_modules
/vendor
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CHANGELOG

[v1.0.0](https://github.com/graph-gophers/graphql-go/releases/tag/v1.0.0) Initial release
13 changes: 13 additions & 0 deletions decode/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package decode

// Unmarshaler defines the api of Go types mapped to custom GraphQL scalar types
type Unmarshaler interface {
// ImplementsGraphQLType maps the implementing custom Go type
// to the GraphQL scalar type in the schema.
ImplementsGraphQLType(name string) bool
// UnmarshalGraphQL is the custom unmarshaler for the implementing type
//
// This function will be called whenever you use the
// custom GraphQL scalar type as an input
UnmarshalGraphQL(input interface{}) error
}
17 changes: 17 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type QueryError struct {
Err error `json:"-"` // Err holds underlying if available
Message string `json:"message"`
Locations []Location `json:"locations,omitempty"`
Path []interface{} `json:"path,omitempty"`
Expand All @@ -23,7 +24,16 @@ func (a Location) Before(b Location) bool {
}

func Errorf(format string, a ...interface{}) *QueryError {
// similar to fmt.Errorf, Errorf will wrap the last argument if it is an instance of error
var err error
if n := len(a); n > 0 {
if v, ok := a[n-1].(error); ok {
err = v
}
}

return &QueryError{
Err: err,
Message: fmt.Sprintf(format, a...),
}
}
Expand All @@ -39,4 +49,11 @@ func (err *QueryError) Error() string {
return str
}

func (err *QueryError) Unwrap() error {
if err == nil {
return nil
}
return err.Err
}

var _ error = &QueryError{}
55 changes: 55 additions & 0 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package errors

import (
"io"
"testing"
)

// Is is simplified facsimile of the go 1.13 errors.Is to ensure QueryError is compatible
func Is(err, target error) bool {
for err != nil {
if target == err {
return true
}

switch e := err.(type) {
case interface{ Unwrap() error }:
err = e.Unwrap()
default:
break
}
}
return false
}

func TestErrorf(t *testing.T) {
cause := io.EOF

t.Run("wrap error", func(t *testing.T) {
err := Errorf("boom: %v", cause)
if !Is(err, cause) {
t.Fatalf("expected errors.Is to return true")
}
})

t.Run("handles nil", func(t *testing.T) {
var err *QueryError
if Is(err, cause) {
t.Fatalf("expected errors.Is to return false")
}
})

t.Run("handle no arguments", func(t *testing.T) {
err := Errorf("boom")
if Is(err, cause) {
t.Fatalf("expected errors.Is to return false")
}
})

t.Run("handle non-error argument arguments", func(t *testing.T) {
err := Errorf("boom: %v", "shaka")
if Is(err, cause) {
t.Fatalf("expected errors.Is to return false")
}
})
}
4 changes: 2 additions & 2 deletions example/social/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ A simple example of how to use struct fields as resolvers instead of methods.

To run this server

`go run ./example/field-resolvers/server/server.go`
`go run ./example/social/server/server.go`

and go to localhost:9011 to interact
and go to localhost:9011 to interact
6 changes: 6 additions & 0 deletions gqltesting/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ func checkErrors(t *testing.T, want, got []*errors.QueryError) {
sortErrors(want)
sortErrors(got)

// Clear the underlying error before the DeepEqual check. It's too
// much to ask the tester to include the raw failing error.
for _, err := range got {
err.Err = nil
}

if !reflect.DeepEqual(got, want) {
t.Fatalf("unexpected error: got %+v, want %+v", got, want)
}
Expand Down
15 changes: 10 additions & 5 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/tribunadigital/graphql-go/introspection"
"github.com/tribunadigital/graphql-go/log"
"github.com/tribunadigital/graphql-go/trace"
"github.com/tribunadigital/graphql-go/types"
)

// ParseSchema parses a GraphQL schema and attaches the given root resolver. It returns an error if
Expand All @@ -42,7 +43,7 @@ func ParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) (
}
}

if err := s.schema.Parse(schemaString, s.useStringDescriptions); err != nil {
if err := schema.Parse(s.schema, schemaString, s.useStringDescriptions); err != nil {
return nil, err
}
if err := s.validateSchema(); err != nil {
Expand All @@ -69,7 +70,7 @@ func MustParseSchema(schemaString string, resolver interface{}, opts ...SchemaOp

// Schema represents a GraphQL schema with an optional resolver.
type Schema struct {
schema *schema.Schema
schema *types.Schema
res *resolvable.Schema

maxDepth int
Expand All @@ -84,6 +85,10 @@ type Schema struct {
extendRes map[string]interface{}
}

func (s *Schema) ASTSchema() *types.Schema {
return s.schema
}

// SchemaOpt is an option to pass to ParseSchema or MustParseSchema.
type SchemaOpt func(*Schema)

Expand Down Expand Up @@ -236,7 +241,7 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str
}
for _, v := range op.Vars {
if _, ok := variables[v.Name.Name]; !ok && v.Default != nil {
variables[v.Name.Name] = v.Default.Value(nil)
variables[v.Name.Name] = v.Default.Deserialize(nil)
}
}

Expand Down Expand Up @@ -296,7 +301,7 @@ func (t *validationBridgingTracer) TraceValidation(context.Context) trace.TraceV
return t.tracer.TraceValidation()
}

func validateRootOp(s *schema.Schema, name string, mandatory bool) error {
func validateRootOp(s *types.Schema, name string, mandatory bool) error {
t, ok := s.EntryPoints[name]
if !ok {
if mandatory {
Expand All @@ -310,7 +315,7 @@ func validateRootOp(s *schema.Schema, name string, mandatory bool) error {
return nil
}

func getOperation(document *query.Document, operationName string) (*query.Operation, error) {
func getOperation(document *types.ExecutableDefinition, operationName string) (*types.OperationDefinition, error) {
if len(document.Operations) == 0 {
return nil, fmt.Errorf("no operations in query document")
}
Expand Down
24 changes: 5 additions & 19 deletions internal/common/directive.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
package common

type Directive struct {
Name Ident
Args ArgumentList
}
import "github.com/tribunadigital/graphql-go/types"

func ParseDirectives(l *Lexer) DirectiveList {
var directives DirectiveList
func ParseDirectives(l *Lexer) types.DirectiveList {
var directives types.DirectiveList
for l.Peek() == '@' {
l.ConsumeToken('@')
d := &Directive{}
d := &types.Directive{}
d.Name = l.ConsumeIdentWithLoc()
d.Name.Loc.Column--
if l.Peek() == '(' {
d.Args = ParseArguments(l)
d.Arguments = ParseArgumentList(l)
}
directives = append(directives, d)
}
return directives
}

type DirectiveList []*Directive

func (l DirectiveList) Get(name string) *Directive {
for _, d := range l {
if d.Name.Name == name {
return d
}
}
return nil
}
10 changes: 5 additions & 5 deletions internal/common/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/scanner"

"github.com/tribunadigital/graphql-go/errors"
"github.com/tribunadigital/graphql-go/types"
)

type syntaxError string
Expand All @@ -30,7 +31,6 @@ func NewLexer(s string, useStringDescriptions bool) *Lexer {
}
sc.Init(strings.NewReader(s))


l := Lexer{sc: sc, useStringDescriptions: useStringDescriptions}
l.sc.Error = l.CatchScannerError

Expand Down Expand Up @@ -119,11 +119,11 @@ func (l *Lexer) ConsumeIdent() string {
return name
}

func (l *Lexer) ConsumeIdentWithLoc() Ident {
func (l *Lexer) ConsumeIdentWithLoc() types.Ident {
loc := l.Location()
name := l.sc.TokenText()
l.ConsumeToken(scanner.Ident)
return Ident{name, loc}
return types.Ident{name, loc}
}

func (l *Lexer) ConsumeKeyword(keyword string) {
Expand All @@ -133,8 +133,8 @@ func (l *Lexer) ConsumeKeyword(keyword string) {
l.ConsumeWhitespace()
}

func (l *Lexer) ConsumeLiteral() *BasicLit {
lit := &BasicLit{Type: l.next, Text: l.sc.TokenText()}
func (l *Lexer) ConsumeLiteral() *types.PrimitiveValue {
lit := &types.PrimitiveValue{Type: l.next, Text: l.sc.TokenText()}
l.ConsumeWhitespace()
return lit
}
Expand Down
Loading

0 comments on commit fd2970f

Please sign in to comment.