Skip to content

Commit 1ef07e4

Browse files
authored
Merge branch 'master' into master
2 parents cbea67d + 69bb0b7 commit 1ef07e4

11 files changed

+162
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ For more complex examples, refer to the [examples/](https://github.com/graphql-g
7171
| [dataloader](https://github.com/nicksrandall/dataloader) | [Nick Randall](https://github.com/nicksrandall) | [DataLoader](https://github.com/facebook/dataloader) implementation in Go. |
7272

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

executor.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"reflect"
8+
"sort"
89
"strings"
910

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

256257
finalResults := make(map[string]interface{}, len(p.Fields))
257-
for responseName, fieldASTs := range p.Fields {
258+
for _, orderedField := range orderedFields(p.Fields) {
259+
responseName := orderedField.responseName
260+
fieldASTs := orderedField.fieldASTs
258261
fieldPath := p.Path.WithKey(responseName)
259262
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs, fieldPath)
260263
if state.hasNoFieldDefs {
@@ -650,15 +653,15 @@ func resolveField(eCtx *executionContext, parentType *Object, source interface{}
650653
Context: eCtx.Context,
651654
})
652655

653-
if resolveFnError != nil {
654-
panic(resolveFnError)
655-
}
656-
657656
extErrs = resolveFieldFinishFn(result, resolveFnError)
658657
if len(extErrs) != 0 {
659658
eCtx.Errors = append(eCtx.Errors, extErrs...)
660659
}
661660

661+
if resolveFnError != nil {
662+
panic(resolveFnError)
663+
}
664+
662665
completed := completeValueCatchingError(eCtx, returnType, fieldASTs, info, path, result)
663666
return completed, resultState
664667
}
@@ -1038,3 +1041,39 @@ func getFieldDef(schema Schema, parentType *Object, fieldName string) *FieldDefi
10381041
}
10391042
return parentType.Fields()[fieldName]
10401043
}
1044+
1045+
// contains field information that will be placed in an ordered slice
1046+
type orderedField struct {
1047+
responseName string
1048+
fieldASTs []*ast.Field
1049+
}
1050+
1051+
// orders fields from a fields map by location in the source
1052+
func orderedFields(fields map[string][]*ast.Field) []*orderedField {
1053+
orderedFields := []*orderedField{}
1054+
fieldMap := map[int]*orderedField{}
1055+
startLocs := []int{}
1056+
1057+
for responseName, fieldASTs := range fields {
1058+
// find the lowest location in the current fieldASTs
1059+
lowest := -1
1060+
for _, fieldAST := range fieldASTs {
1061+
loc := fieldAST.GetLoc().Start
1062+
if lowest == -1 || loc < lowest {
1063+
lowest = loc
1064+
}
1065+
}
1066+
startLocs = append(startLocs, lowest)
1067+
fieldMap[lowest] = &orderedField{
1068+
responseName: responseName,
1069+
fieldASTs: fieldASTs,
1070+
}
1071+
}
1072+
1073+
sort.Ints(startLocs)
1074+
for _, startLoc := range startLocs {
1075+
orderedFields = append(orderedFields, fieldMap[startLoc])
1076+
}
1077+
1078+
return orderedFields
1079+
}

extensions_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ func tinit(t *testing.T) graphql.Schema {
2323
return "foo", nil
2424
},
2525
},
26+
"erred": &graphql.Field{
27+
Type: graphql.String,
28+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
29+
return "", errors.New("ooops")
30+
},
31+
},
2632
},
2733
}),
2834
})
@@ -306,6 +312,35 @@ func TestExtensionResolveFieldFinishFuncPanic(t *testing.T) {
306312
}
307313
}
308314

315+
func TestExtensionResolveFieldFinishFuncAfterError(t *testing.T) {
316+
var fnErrs int
317+
ext := newtestExt("testExt")
318+
ext.resolveFieldDidStartFn = func(ctx context.Context, i *graphql.ResolveInfo) (context.Context, graphql.ResolveFieldFinishFunc) {
319+
return ctx, func(v interface{}, err error) {
320+
if err != nil {
321+
fnErrs++
322+
}
323+
}
324+
}
325+
326+
schema := tinit(t)
327+
query := `query Example { erred }`
328+
schema.AddExtensions(ext)
329+
330+
result := graphql.Do(graphql.Params{
331+
Schema: schema,
332+
RequestString: query,
333+
})
334+
335+
if resErrs := len(result.Errors); resErrs != 1 {
336+
t.Errorf("Incorrect number of returned result errors: %d", resErrs)
337+
}
338+
339+
if fnErrs != 1 {
340+
t.Errorf("Incorrect number of errors captured: %d", fnErrs)
341+
}
342+
}
343+
309344
func TestExtensionGetResultPanic(t *testing.T) {
310345
ext := newtestExt("testExt")
311346
ext.getResultFn = func(context.Context) interface{} {

introspection.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ func init() {
312312
},
313313
"deprecationReason": &Field{
314314
Type: String,
315+
Resolve: func(p ResolveParams) (interface{}, error) {
316+
if field, ok := p.Source.(*FieldDefinition); ok {
317+
if field.DeprecationReason != "" {
318+
return field.DeprecationReason, nil
319+
}
320+
}
321+
return nil, nil
322+
},
315323
},
316324
},
317325
})
@@ -497,6 +505,14 @@ func init() {
497505
},
498506
"deprecationReason": &Field{
499507
Type: String,
508+
Resolve: func(p ResolveParams) (interface{}, error) {
509+
if field, ok := p.Source.(*EnumValueDefinition); ok {
510+
if field.DeprecationReason != "" {
511+
return field.DeprecationReason, nil
512+
}
513+
}
514+
return nil, nil
515+
},
500516
},
501517
},
502518
})

0 commit comments

Comments
 (0)