defaultFieldResolver does not resolve field alias
See original GitHub issueCurrently 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.
This issue is also related to https://github.com/apollographql/graphql-tools/issues/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?
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (5 by maintainers)

Top Related StackOverflow Question
@alvis Great to hear! Proxying GraphQL requests are a pretty new field, so not everything works as expected. For example, you can break the majority of proxies with a query like that:
And it’s not a
graphql-jsor GraphQL specification problem it just one more case that proxy needs to handle.@alvis I’m confused, so let’s go step by step based on my example:
mefield it returns{ name: 'Ivan' }and this value becomessource.namedoesn’t have resolver sodefaultFieldResolveris called with args:source-{ name: 'Ivan' }info- wherefieldNameisnamebutinfo.path.keyisfirstName.const property = source[info.fieldName];sopropertybecameIvan.const property = source[info.path.key];sopropertybecameundefined.defaultFieldResolverresolver returnspropertyand it becomes value in the responce.