Skip to content

defaultFieldResolver does not resolve field alias #1360

@alvis

Description

@alvis

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.

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions