Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

refactor: migrate to use upstream GraphQL #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 3 additions & 9 deletions customtypes/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package customtypes
import (
"errors"
"strconv"

graphql "github.com/graph-gophers/graphql-go"
)

// ID represents GraphQL's "ID" scalar type. A custom type may be used instead.
type ID string

func (ID) ImplementsGraphQLType(name string) bool {
return name == "ID"
}
type ID graphql.ID

func (id *ID) UnmarshalGraphQL(input interface{}) error {
var err error
Expand All @@ -24,7 +22,3 @@ func (id *ID) UnmarshalGraphQL(input interface{}) error {
}
return err
}

func (id ID) MarshalJSON() ([]byte, error) {
return strconv.AppendQuote(nil, string(id)), nil
}
23 changes: 3 additions & 20 deletions customtypes/time.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package customtypes

import (
"encoding/json"
"fmt"
"time"
)

// Time is a custom GraphQL type to represent an instant in time. It has to be added to a schema
// via "scalar Time" since it is not a predeclared GraphQL type like "ID".
type Time struct {
time.Time
}
graphql "github.com/graph-gophers/graphql-go"
)

// ImplementsGraphQLType maps this custom Go type
// to the graphql scalar type in the schema.
func (Time) ImplementsGraphQLType(name string) bool {
return name == "Time"
}
type Time graphql.Time

// UnmarshalGraphQL is a custom unmarshaler for Time
//
Expand All @@ -41,11 +32,3 @@ func (t *Time) UnmarshalGraphQL(input interface{}) error {
return fmt.Errorf("wrong type")
}
}

// MarshalJSON is a custom marshaler for Time
//
// This function will be called whenever you
// query for fields that use the Time type
func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Time)
}
5 changes: 4 additions & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/chirino/graphql/resolvers"
"github.com/chirino/graphql/schema"
"github.com/chirino/graphql/trace"
upgraphql "github.com/graph-gophers/graphql-go"
)

type Engine struct {
Expand Down Expand Up @@ -142,7 +143,9 @@ func (engine *Engine) ServeGraphQLStream(request *Request) ResponseStream {
TryCast: engine.TryCast,
FireSubscriptionEventFunc: func(d json.RawMessage, e qerrors.ErrorList) {
responses <- &Response{
Data: d,
Response: &upgraphql.Response {
Data: d,
},
Errors: e,
}
traceResponse(e)
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ func NewError(message string) *Error {
}

func Errorf(format string, a ...interface{}) *Error {
return qerrors.Errorf(format, a...)
return qerrors.New(format, a...)
}
2 changes: 1 addition & 1 deletion exec/field_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c FieldSelectionContext) Apply(selections []schema.Selection) (result []Fi

field := fields.Get(selection.Name)
if field == nil {
errs = append(errs, qerrors2.Errorf("field '%s' not found on '%s': ", selection.Name, c.OnType.String()))
errs = append(errs, qerrors2.New("field '%s' not found on '%s': ", selection.Name, c.OnType.String()))
} else {
selection.Schema = &schema.FieldSchema{
Field: field,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/friendsofgo/graphiql v0.2.2
github.com/gorilla/websocket v1.4.2
github.com/graph-gophers/graphql-go v0.0.0-20210319060855-d2656e8bde15
github.com/josharian/intern v1.0.0
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.9.1
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/friendsofgo/graphiql v0.2.2 h1:ccnuxpjgIkB+Lr9YB2ZouiZm7wvciSfqwpa9ug
github.com/friendsofgo/graphiql v0.2.2/go.mod h1:8Y2kZ36AoTGWs78+VRpvATyt3LJBx0SZXmay80ZTRWo=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/graph-gophers/graphql-go v0.0.0-20210319060855-d2656e8bde15 h1:ys+YCFVAWiVMaaZQ2nNtSwmYTaqdw1d+rpw04JJmIWE=
github.com/graph-gophers/graphql-go v0.0.0-20210319060855-d2656e8bde15/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
Expand All @@ -21,7 +23,6 @@ github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06B
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
Expand All @@ -37,7 +38,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -48,7 +48,6 @@ golang.org/x/tools v0.0.0-20200128220307-520188d60f50/go.mod h1:TB2adYChydJhpapK
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 1 addition & 1 deletion httpgql/http-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ func (client *Client) ServeGraphQL(request *graphql.Request) *graphql.Response {
}

preview, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1024))
return response.AddError(qerrors.Errorf("invalid content type: %s", contentType).WithCause(errors.New(string(preview))))
return response.AddError(qerrors.New("invalid content type: %s", contentType).WithCause(errors.New(string(preview))))
}
5 changes: 4 additions & 1 deletion httpgql/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/chirino/graphql"
"github.com/chirino/graphql/internal/example/starwars"
upgraphql "github.com/graph-gophers/graphql-go"
)

func TestEngineAPIServeHTTP(t *testing.T) {
Expand All @@ -38,7 +39,9 @@ func TestClientServeGraphQL(t *testing.T) {
s := httptest.NewServer(&httpgql.Handler{
ServeGraphQL: func(request *graphql.Request) *graphql.Response {
return &graphql.Response{
Data: json.RawMessage(`{"hello":"world"}`),
Response: &upgraphql.Response {
Data: json.RawMessage(`{"hello":"world"}`),
},
}
},
})
Expand Down
2 changes: 1 addition & 1 deletion httpgql/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func UnmarshalSpec(id customtypes.ID, v interface{}) error {
}
i := strings.IndexByte(string(s), ':')
if i == -1 {
return qerrors.Errorf("invalid graphql.ID")
return qerrors.New("invalid graphql.ID")
}
return json.Unmarshal([]byte(s[i+1:]), v)
}
9 changes: 7 additions & 2 deletions httpgql/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/chirino/graphql"
"github.com/chirino/graphql/httpgql"
upgraphql "github.com/graph-gophers/graphql-go"
"github.com/stretchr/testify/assert"
)

Expand All @@ -26,10 +27,14 @@ func TestClientServeGraphQLStream(t *testing.T) {
ServeGraphQLStream: func(request *graphql.Request) graphql.ResponseStream {
result := make(chan *graphql.Response, 2)
result <- &graphql.Response{
Data: json.RawMessage(`{"hello":"world"}`),
Response: &upgraphql.Response {
Data: json.RawMessage(`{"hello":"world"}`),
},
}
result <- &graphql.Response{
Data: json.RawMessage(`{"bye":"world"}`),
Response: &upgraphql.Response {
Data: json.RawMessage(`{"bye":"world"}`),
},
}
close(result)
return result
Expand Down
29 changes: 18 additions & 11 deletions internal/exec/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/chirino/graphql/resolvers"
"github.com/chirino/graphql/schema"
"github.com/chirino/graphql/trace"
uperrors "github.com/graph-gophers/graphql-go/errors"
)

type Execution struct {
Expand Down Expand Up @@ -78,14 +79,14 @@ func (this *Execution) HandlePanic(path []string) error {
if value := recover(); value != nil {
this.Logger.LogPanic(this.Context, value)
err := makePanicError(value)
err.Path = path
err.PathStr = path
return err
}
return nil
}

func makePanicError(value interface{}) *qerrors.Error {
return qerrors.Errorf("graphql: panic occurred: %v", value)
return qerrors.New("graphql: panic occurred: %v", value)
}

type ExecutionResult struct {
Expand Down Expand Up @@ -155,8 +156,10 @@ func (this *Execution) resolveFields(ctx context.Context, parentSelectionResolve

if resolution == nil {
this.AddError((&qerrors.Error{
Message: "No resolver found",
Path: append(parentSelectionResolver.Path(), field.Alias),
QueryError: &uperrors.QueryError {
Message: "No resolver found",
},
PathStr: append(parentSelectionResolver.Path(), field.Alias),
}).WithStack())
} else {
sr.Resolution = resolution
Expand Down Expand Up @@ -213,7 +216,7 @@ func (this *Execution) Execute() error {
// subs are kinda special... we need to setup a subscription to an event,
// and execute it every time the event is triggered...
if len(this.Operation.Selections) != 1 {
return qerrors.Errorf("you can only select 1 field in a subscription")
return qerrors.New("you can only select 1 field in a subscription")
}

if len(rootFields.ValuesByKey) != 1 {
Expand Down Expand Up @@ -321,7 +324,7 @@ func (this *Execution) executeSelected(ctx context.Context, parentSelection *Sel
if value := recover(); value != nil {
this.Logger.LogPanic(this.Context, value)
err := makePanicError(value)
err.Path = selected.Path()
err.PathStr = selected.Path()
result = err
}
}()
Expand Down Expand Up @@ -362,8 +365,10 @@ func (this *Execution) executeSelected(ctx context.Context, parentSelection *Sel
if !valid || ((childValue.Kind() == reflect.Ptr || childValue.Kind() == reflect.Interface) && childValue.IsNil()) {
if nonNullType {
return (&qerrors.Error{
Message: "ResolverFactory produced a nil value for a Non Null type",
Path: selected.Path(),
QueryError: &uperrors.QueryError {
Message: "ResolverFactory produced a nil value for a Non Null type",
},
PathStr: selected.Path(),
}).WithStack()
} else {
this.data.WriteString("null")
Expand Down Expand Up @@ -439,15 +444,15 @@ func (this *Execution) writeLeaf(childValue reflect.Value, selectionResolver *Se
switch childType := childType.(type) {
case *schema.NonNull:
if childValue.Kind() == reflect.Ptr && childValue.Elem().IsNil() {
panic(qerrors.Errorf("got nil for non-null %q", childType))
panic(qerrors.New("got nil for non-null %q", childType))
} else {
this.writeLeaf(childValue, selectionResolver, childType.OfType)
}

case *schema.Scalar:
data, err := json.Marshal(childValue.Interface())
if err != nil {
panic(qerrors.Errorf("could not marshal %v: %s", childValue, err))
panic(qerrors.New("could not marshal %v: %s", childValue, err))
}
this.data.Write(data)

Expand Down Expand Up @@ -480,7 +485,9 @@ func (r *Execution) AddError(err error) {
qe = err
default:
qe = &qerrors.Error{
Message: err.Error(),
QueryError: &uperrors.QueryError {
Message: err.Error(),
},
}
}
r.Mu.Lock()
Expand Down
6 changes: 3 additions & 3 deletions internal/exec/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ type structPackerField struct {

func (p *StructPacker) Pack(value interface{}) (reflect.Value, error) {
if value == nil {
return reflect.Value{}, qerrors.Errorf("got null for non-null")
return reflect.Value{}, qerrors.New("got null for non-null")
}

values := value.(map[string]interface{})
Expand Down Expand Up @@ -289,7 +289,7 @@ type ValuePacker struct {

func (p *ValuePacker) Pack(value interface{}) (reflect.Value, error) {
if value == nil {
return reflect.Value{}, qerrors.Errorf("got null for non-null")
return reflect.Value{}, qerrors.New("got null for non-null")
}

coerced, err := unmarshalInput(p.ValueType, value)
Expand All @@ -305,7 +305,7 @@ type unmarshalerPacker struct {

func (p *unmarshalerPacker) Pack(value interface{}) (reflect.Value, error) {
if value == nil {
return reflect.Value{}, qerrors.Errorf("got null for non-null")
return reflect.Value{}, qerrors.New("got null for non-null")
}

v := reflect.New(p.ValueType)
Expand Down
5 changes: 3 additions & 2 deletions internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"github.com/chirino/graphql/internal/scanner"
"github.com/chirino/graphql/qerrors"
uperrors "github.com/graph-gophers/graphql-go/errors"

"strconv"
"strings"
)

type syntaxError string
type Location = qerrors.Location
type Location = uperrors.Location

type Lexer struct {
sc scanner.Scanner
Expand Down Expand Up @@ -40,7 +41,7 @@ func (l *Lexer) CatchSyntaxError(f func()) (errRes error) {
defer func() {
if err := recover(); err != nil {
if err, ok := err.(syntaxError); ok {
errRes = qerrors.Errorf("syntax error: %s", err).WithLocations(l.Location())
errRes = qerrors.New("syntax error: %s", err).WithLocations(l.Location())
return
}
panic(err)
Expand Down
Loading