Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 29, 2016
1 parent c28891d commit fcfa135
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
32 changes: 3 additions & 29 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"

"github.com/opentracing/opentracing-go"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"

"github.com/neelance/graphql-go/errors"
Expand Down Expand Up @@ -39,39 +39,13 @@ type Response struct {
}

func (s *Schema) Exec(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response {
d, err := query.Parse(queryString)
document, err := query.Parse(queryString)
if err != nil {
return &Response{
Errors: []*errors.GraphQLError{err},
}
}

if len(d.Operations) == 0 {
return &Response{
Errors: []*errors.GraphQLError{errors.Errorf("no operations in query document")},
}
}

var op *query.Operation
if operationName == "" {
if len(d.Operations) > 1 {
return &Response{
Errors: []*errors.GraphQLError{errors.Errorf("more than one operation in query document and no operation name given")},
}
}
for _, op2 := range d.Operations {
op = op2
}
} else {
var ok bool
op, ok = d.Operations[operationName]
if !ok {
return &Response{
Errors: []*errors.GraphQLError{errors.Errorf("no operation with name %q", operationName)},
}
}
}

span, subCtx := opentracing.StartSpanFromContext(ctx, "GraphQL request")
span.SetTag("query", queryString)
if operationName != "" {
Expand All @@ -82,7 +56,7 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
}
defer span.Finish()

data, errs := s.exec.Exec(subCtx, d, variables, op)
data, errs := exec.ExecuteRequest(subCtx, s.exec, document, operationName, variables)
if len(errs) != 0 {
ext.Error.Set(span, true)
span.SetTag("errorMsg", errs)
Expand Down
33 changes: 31 additions & 2 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,29 +396,58 @@ func (r *request) handlePanic() {
}
}

func (e *Exec) Exec(ctx context.Context, document *query.Document, variables map[string]interface{}, op *query.Operation) (interface{}, []*errors.GraphQLError) {
func ExecuteRequest(ctx context.Context, e *Exec, document *query.Document, operationName string, variables map[string]interface{}) (interface{}, []*errors.GraphQLError) {
op, err := getOperation(document, operationName)
if err != nil {
return nil, []*errors.GraphQLError{err}
}

r := &request{
doc: document,
vars: variables,
schema: e.schema,
}

var opExec iExec
var serially bool
switch op.Type {
case query.Query:
opExec = e.queryExec
serially = false
case query.Mutation:
opExec = e.mutationExec
serially = true
}

data := func() interface{} {
defer r.handlePanic()
return opExec.exec(ctx, r, op.SelSet, e.resolver, op.Type == query.Mutation)
return opExec.exec(ctx, r, op.SelSet, e.resolver, serially)
}()

return data, r.errs
}

func getOperation(document *query.Document, operationName string) (*query.Operation, *errors.GraphQLError) {
if len(document.Operations) == 0 {
return nil, errors.Errorf("no operations in query document")
}

if operationName == "" {
if len(document.Operations) > 1 {
return nil, errors.Errorf("more than one operation in query document and no operation name given")
}
for _, op := range document.Operations {
return op, nil // return the one and only operation
}
}

op, ok := document.Operations[operationName]
if !ok {
return nil, errors.Errorf("no operation with name %q", operationName)
}
return op, nil
}

type iExec interface {
exec(ctx context.Context, r *request, selSet *query.SelectionSet, resolver reflect.Value, serially bool) interface{}
}
Expand Down

0 comments on commit fcfa135

Please sign in to comment.