-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Currently defaultFieldResolver is used in graphql-tools as the default resolver. However, it doesn't work with aliased field. e.g. The first example works but not the second one. In the second case, nickname would be resolved to null rather than being identical to name.
query {
me {
name
}
}query {
me {
nickname: name
}
}The cause of the issue is that the current resolver only takes the fieldName from info rather than any aliased name in line 1232.
graphql-js/src/execution/execute.js
Lines 1224 to 1238 in ef585e9
| export const defaultFieldResolver: GraphQLFieldResolver<any, *> = function( | |
| source, | |
| args, | |
| contextValue, | |
| info, | |
| ) { | |
| // ensure source is a value for which property access is acceptable. | |
| if (typeof source === 'object' || typeof source === 'function') { | |
| const property = source[info.fieldName]; | |
| if (typeof property === 'function') { | |
| return source[info.fieldName](args, contextValue, info); | |
| } | |
| return property; | |
| } | |
| }; |
This issue is also related to ardatan/graphql-tools#519, which leads to a custom solution (doc) in https://github.com/apollographql/graphql-tools/blob/v3.0.0/src/stitching/defaultMergedResolver.ts
I believe that we should fix this at the root. Changing info.fieldName to info.path.key or info.fieldNodes[0].alias.value seems to be non-breaking and yet a proper fix for supporting field alias. If agreed, I'd like to make a PR for that.
@robinhood: Do you have any opinion on the change?