Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon authored Feb 5, 2020
2 parents cbea67d + 69bb0b7 commit 1ef07e4
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ For more complex examples, refer to the [examples/](https://github.com/graphql-g
| [dataloader](https://github.com/nicksrandall/dataloader) | [Nick Randall](https://github.com/nicksrandall) | [DataLoader](https://github.com/facebook/dataloader) implementation in Go. |

### Blog Posts
- [Golang + GraphQL + Relay](http://wehavefaces.net/)
- [Golang + GraphQL + Relay](https://wehavefaces.net/learn-golang-graphql-relay-1-e59ea174a902)

49 changes: 44 additions & 5 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"sort"
"strings"

"github.com/graphql-go/graphql/gqlerrors"
Expand Down Expand Up @@ -254,7 +255,9 @@ func executeFieldsSerially(p executeFieldsParams) *Result {
}

finalResults := make(map[string]interface{}, len(p.Fields))
for responseName, fieldASTs := range p.Fields {
for _, orderedField := range orderedFields(p.Fields) {
responseName := orderedField.responseName
fieldASTs := orderedField.fieldASTs
fieldPath := p.Path.WithKey(responseName)
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs, fieldPath)
if state.hasNoFieldDefs {
Expand Down Expand Up @@ -650,15 +653,15 @@ func resolveField(eCtx *executionContext, parentType *Object, source interface{}
Context: eCtx.Context,
})

if resolveFnError != nil {
panic(resolveFnError)
}

extErrs = resolveFieldFinishFn(result, resolveFnError)
if len(extErrs) != 0 {
eCtx.Errors = append(eCtx.Errors, extErrs...)
}

if resolveFnError != nil {
panic(resolveFnError)
}

completed := completeValueCatchingError(eCtx, returnType, fieldASTs, info, path, result)
return completed, resultState
}
Expand Down Expand Up @@ -1038,3 +1041,39 @@ func getFieldDef(schema Schema, parentType *Object, fieldName string) *FieldDefi
}
return parentType.Fields()[fieldName]
}

// contains field information that will be placed in an ordered slice
type orderedField struct {
responseName string
fieldASTs []*ast.Field
}

// orders fields from a fields map by location in the source
func orderedFields(fields map[string][]*ast.Field) []*orderedField {
orderedFields := []*orderedField{}
fieldMap := map[int]*orderedField{}
startLocs := []int{}

for responseName, fieldASTs := range fields {
// find the lowest location in the current fieldASTs
lowest := -1
for _, fieldAST := range fieldASTs {
loc := fieldAST.GetLoc().Start
if lowest == -1 || loc < lowest {
lowest = loc
}
}
startLocs = append(startLocs, lowest)
fieldMap[lowest] = &orderedField{
responseName: responseName,
fieldASTs: fieldASTs,
}
}

sort.Ints(startLocs)
for _, startLoc := range startLocs {
orderedFields = append(orderedFields, fieldMap[startLoc])
}

return orderedFields
}
35 changes: 35 additions & 0 deletions extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func tinit(t *testing.T) graphql.Schema {
return "foo", nil
},
},
"erred": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "", errors.New("ooops")
},
},
},
}),
})
Expand Down Expand Up @@ -306,6 +312,35 @@ func TestExtensionResolveFieldFinishFuncPanic(t *testing.T) {
}
}

func TestExtensionResolveFieldFinishFuncAfterError(t *testing.T) {
var fnErrs int
ext := newtestExt("testExt")
ext.resolveFieldDidStartFn = func(ctx context.Context, i *graphql.ResolveInfo) (context.Context, graphql.ResolveFieldFinishFunc) {
return ctx, func(v interface{}, err error) {
if err != nil {
fnErrs++
}
}
}

schema := tinit(t)
query := `query Example { erred }`
schema.AddExtensions(ext)

result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})

if resErrs := len(result.Errors); resErrs != 1 {
t.Errorf("Incorrect number of returned result errors: %d", resErrs)
}

if fnErrs != 1 {
t.Errorf("Incorrect number of errors captured: %d", fnErrs)
}
}

func TestExtensionGetResultPanic(t *testing.T) {
ext := newtestExt("testExt")
ext.getResultFn = func(context.Context) interface{} {
Expand Down
16 changes: 16 additions & 0 deletions introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ func init() {
},
"deprecationReason": &Field{
Type: String,
Resolve: func(p ResolveParams) (interface{}, error) {
if field, ok := p.Source.(*FieldDefinition); ok {
if field.DeprecationReason != "" {
return field.DeprecationReason, nil
}
}
return nil, nil
},
},
},
})
Expand Down Expand Up @@ -497,6 +505,14 @@ func init() {
},
"deprecationReason": &Field{
Type: String,
Resolve: func(p ResolveParams) (interface{}, error) {
if field, ok := p.Source.(*EnumValueDefinition); ok {
if field.DeprecationReason != "" {
return field.DeprecationReason, nil
}
}
return nil, nil
},
},
},
})
Expand Down
Loading

0 comments on commit 1ef07e4

Please sign in to comment.