question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

defaultFieldResolver does not resolve field alias

See original GitHub issue

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.

https://github.com/graphql/graphql-js/blob/ef585e9db8d161e715dfaa4e70c1ed2efa58294c/src/execution/execute.js#L1224-L1238

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:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
IvanGoncharovcommented, May 30, 2018

Thanks for explaining. It’s clear to me now.

@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:

{
  interfaceOrUnion {
    __typename: someField
  }
}

And it’s not a graphql-js or GraphQL specification problem it just one more case that proxy needs to handle.

1reaction
IvanGoncharovcommented, May 30, 2018

@alvis I’m confused, so let’s go step by step based on my example:

  1. Resolver called for me field it returns { name: 'Ivan' } and this value becomes source.
  2. name doesn’t have resolver so defaultFieldResolver is called with args:
  • source - { name: 'Ivan' }
  • info - where fieldName is name but info.path.key is firstName.
  1. Here is where current and proposed behavior differs:
  • Current: const property = source[info.fieldName]; so property became Ivan.
  • Proposed: const property = source[info.path.key]; so property became undefined.
  1. defaultFieldResolver resolver returns property and it becomes value in the responce.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Selections - Lacinia - Read the Docs
To review: executing a GraphQL query is highly recursive, where a field resolver will be responsible for obtaining data from an external resource...
Read more >
Creating schema directives - Apollo GraphQL Docs
A custom directive can transform a resolved GraphQL field's value before it's returned to the requesting client.
Read more >
graphql - Apollo server does not serialize virtual property from ...
What I already know: Game and Player "id" field is a "virtual" field created by Mongoose as an alias for "_id" which is...
Read more >
Customizing the GraphQL Schema - Gatsby
This guide is aimed at plugin authors, users trying to fix GraphQL ... and therefore does not scale very well and (2) if...
Read more >
graphql-tools/executor
If a resolve function is not given, then a default resolve behavior is used which takes the property of the source object of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found