Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update upstream v1.1.0 #4

Merged
merged 25 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e0b00b8
Create CHANGELOG.md
pavelnikolov Apr 13, 2021
1a8cd9c
Update CHANGELOG.md
pavelnikolov Apr 13, 2021
c46cc71
ignore JetBrains IDEA and vscode meta directories
sGy1980de Apr 20, 2021
04aa634
expose packer.Unmarshaler interface as graphql.Unmarshaler
sGy1980de Apr 20, 2021
60ab3c8
move packer.Unmarshaler interface to decode.Unmarshaler, so the metho…
sGy1980de Apr 20, 2021
21f7aef
Merge pull request #450 from esome/master
pavelnikolov Apr 21, 2021
bf0a0cc
add types package
seansorr Mar 16, 2021
d3bed17
add getter for the types.Schema field
seansorr Mar 17, 2021
d0dc5cd
unused fields
seansorr Mar 18, 2021
31fbb90
rename to match types
seansorr Mar 18, 2021
2af5fa3
remove unused
seansorr Mar 29, 2021
d579b56
use a string and not an Ident for a FieldDefinition's name
seansorr Mar 30, 2021
9d304f9
fix compile errors introduced by ab449f07e
seansorr Mar 30, 2021
502fac2
merge conflict errors
seansorr Apr 22, 2021
8a96404
Merge pull request #437 from rudle/types-leaf-package
pavelnikolov Apr 23, 2021
242dace
add location fields to type definitions
benevolent-donut Apr 27, 2021
5b1abb9
Fix dir in readme
jinleileiking Apr 29, 2021
8967594
Merge pull request #452 from jinleileiking/patch-1
pavelnikolov Apr 29, 2021
c126754
errors.Errorf preserves original error similar to fmt.Error
savaki Apr 30, 2021
d887704
removed test dependency on errors.Is
savaki Apr 30, 2021
ce94028
checkErrors ignores the raw error for purposes of determining if the …
savaki Apr 30, 2021
b8f211c
Merge pull request #454 from ssko1/loc-types
pavelnikolov Apr 30, 2021
4378f8e
Merge pull request #456 from savaki/master
pavelnikolov Apr 30, 2021
04e3415
Update package imports
Feb 4, 2024
542c043
Merge branch 'upstream-v1.1.0-for-merge' into update-upstream-v1.1.0
Feb 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading