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

defaultFieldResolver should bind source #980

Closed

Conversation

dimitri-koenig
Copy link

PR for this issue: defaultFieldResolver should bind source #979

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@@ -1247,7 +1247,8 @@ function (source, args, context, info) {
if (typeof source === 'object' || typeof source === 'function') {
const property = source[info.fieldName];
if (typeof property === 'function') {
return source[info.fieldName](args, context, info);
/* eslint-disable no-useless-call */
return source[info.fieldName].apply(source, [ args, context, info ]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function#call is usually faster than Function#apply, and since you're not passing varargs here, it'd probably be better to go:

return source[info.fieldName].call(source, args, context, info);

@dimitri-koenig
Copy link
Author

Fair enough, there we go with call instead of apply

@dimitri-koenig
Copy link
Author

@papandreou Any more changes needed or is this PR ready to be merged?

@papandreou
Copy link

@dimitri-koenig, I'm not in charge here, so I don't know :)

@leebyron
Copy link
Contributor

Thanks for your patience - could you please add unit tests for this?

I think this was always the intent, and was assuming that the source[method](arg) form would use source as this in the same way as the source.method(arg) form. This assumption is also behind why no-useless-call is being triggered. I'm curious to know if eslint is also wrong, if so we should make sure an issue is filed on that project.

Adding a test for this may help either confirm that the existing code does this, or if it failed with the existing code that this solution is the right fix. Either way, the test would be really valuable.

@dimitri-koenig
Copy link
Author

I found the real issue, though I don't have an explanation for it:

In graphql-tools the same code is being used though an old version, where the call of the class method is being made like this:

return property(args, context);

Changing this to the code used right now works:

return source[info.fieldName](args, context, info);

So no need to change anything.

@dimitri-koenig dimitri-koenig deleted the feature/bind-this branch August 14, 2017 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants