Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

u- return alias field #630

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -628,6 +633,7 @@ func resolveField(eCtx *executionContext, parentType *Object, source interface{}

info := ResolveInfo{
FieldName: fieldName,
FieldNameAlias: fieldNameAlias,
FieldASTs: fieldASTs,
Path: path,
ReturnType: returnType,
Expand Down Expand Up @@ -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
Expand Down