From 424d61d4ff28d77b5af929c9d4acd479975ac28c Mon Sep 17 00:00:00 2001 From: wenj91 <1541683150@qq.com> Date: Thu, 28 Apr 2022 21:38:09 +0800 Subject: [PATCH] u- return alias field --- definition.go | 1 + executor.go | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/definition.go b/definition.go index 0fbbbd52..cda22393 100644 --- a/definition.go +++ b/definition.go @@ -589,6 +589,7 @@ type FieldResolveFn func(p ResolveParams) (interface{}, error) type ResolveInfo struct { FieldName string + FieldNameAlias string FieldASTs []*ast.Field Path *ResponsePath ReturnType Output diff --git a/executor.go b/executor.go index 7440ae21..6e459a56 100644 --- a/executor.go +++ b/executor.go @@ -610,6 +610,11 @@ func resolveField(eCtx *executionContext, parentType *Object, source interface{} fieldName = fieldAST.Name.Value } + fieldNameAlias := "" + if fieldAST.Alias != nil { + fieldNameAlias = fieldAST.Alias.Value + } + fieldDef := getFieldDef(eCtx.Schema, parentType, fieldName) if fieldDef == nil { resultState.hasNoFieldDefs = true @@ -628,6 +633,7 @@ func resolveField(eCtx *executionContext, parentType *Object, source interface{} info := ResolveInfo{ FieldName: fieldName, + FieldNameAlias: fieldNameAlias, FieldASTs: fieldASTs, Path: path, ReturnType: returnType, @@ -982,6 +988,13 @@ func DefaultResolveFn(p ResolveParams) (interface{}, error) { // try p.Source as a map[string]interface if sourceMap, ok := p.Source.(map[string]interface{}); ok { property := sourceMap[p.Info.FieldName] + if p.Info.FieldNameAlias != "" { + propertyAlias, pok := sourceMap[p.Info.FieldNameAlias] + if pok { + property = propertyAlias + } + } + val := reflect.ValueOf(property) if val.IsValid() && val.Type().Kind() == reflect.Func { // try type casting the func to the most basic func signature