Skip to content

Commit 7efae5e

Browse files
committed
Cleaner default resolver, cache FieldDefMap Get, preallocate
1 parent ed4c4d8 commit 7efae5e

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

definition.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ func (gt *Object) String() string {
441441
return gt.PrivateName
442442
}
443443
func (gt *Object) Fields() FieldDefinitionMap {
444+
if len(gt.fields) != 0 {
445+
return gt.fields
446+
}
444447
var configureFields Fields
445448
switch gt.typeConfig.Fields.(type) {
446449
case Fields:

executor.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func executeFields(p ExecuteFieldsParams) *Result {
278278
p.Fields = map[string][]*ast.Field{}
279279
}
280280

281-
finalResults := map[string]interface{}{}
281+
finalResults := make(map[string]interface{}, len(p.Fields))
282282
for responseName, fieldASTs := range p.Fields {
283283
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs)
284284
if state.hasNoFieldDefs {
@@ -741,16 +741,16 @@ func completeObjectValue(eCtx *ExecutionContext, returnType *Object, fieldASTs [
741741
continue
742742
}
743743
selectionSet := fieldAST.SelectionSet
744-
if selectionSet != nil {
745-
innerParams := CollectFieldsParams{
746-
ExeContext: eCtx,
747-
RuntimeType: returnType,
748-
SelectionSet: selectionSet,
749-
Fields: subFieldASTs,
750-
VisitedFragmentNames: visitedFragmentNames,
751-
}
752-
subFieldASTs = collectFields(innerParams)
744+
if selectionSet == nil {
745+
continue
753746
}
747+
subFieldASTs = collectFields(CollectFieldsParams{
748+
ExeContext: eCtx,
749+
RuntimeType: returnType,
750+
SelectionSet: selectionSet,
751+
Fields: subFieldASTs,
752+
VisitedFragmentNames: visitedFragmentNames,
753+
})
754754
}
755755
executeFieldsParams := ExecuteFieldsParams{
756756
ExecutionContext: eCtx,
@@ -790,7 +790,7 @@ func completeListValue(eCtx *ExecutionContext, returnType *List, fieldASTs []*as
790790
}
791791

792792
itemType := returnType.OfType
793-
completedResults := []interface{}{}
793+
completedResults := make([]interface{}, 0, resultVal.Len())
794794
for i := 0; i < resultVal.Len(); i++ {
795795
val := resultVal.Index(i).Interface()
796796
completedItem := completeValueCatchingError(eCtx, itemType, fieldASTs, info, val)
@@ -834,29 +834,18 @@ func DefaultResolveFn(p ResolveParams) (interface{}, error) {
834834
return nil, nil
835835
}
836836
if sourceVal.Type().Kind() == reflect.Struct {
837+
// try matching the field name first
838+
if v := sourceVal.FieldByName(p.Info.FieldName); v.IsValid() {
839+
return v.Interface(), nil
840+
}
841+
var tag reflect.StructTag
837842
for i := 0; i < sourceVal.NumField(); i++ {
838-
valueField := sourceVal.Field(i)
839-
typeField := sourceVal.Type().Field(i)
840-
// try matching the field name first
841-
if typeField.Name == p.Info.FieldName {
842-
return valueField.Interface(), nil
843-
}
844-
tag := typeField.Tag
843+
tag = sourceVal.Type().Field(i).Tag
845844
checkTag := func(tagName string) bool {
846-
t := tag.Get(tagName)
847-
tOptions := strings.Split(t, ",")
848-
if len(tOptions) == 0 {
849-
return false
850-
}
851-
if tOptions[0] != p.Info.FieldName {
852-
return false
853-
}
854-
return true
845+
return strings.SplitN(tag.Get(tagName), ",", 1)[0] == p.Info.FieldName
855846
}
856847
if checkTag("json") || checkTag("graphql") {
857-
return valueField.Interface(), nil
858-
} else {
859-
continue
848+
return sourceVal.Field(i).Interface(), nil
860849
}
861850
}
862851
return nil, nil

0 commit comments

Comments
 (0)