Use {} instead of Object.create(null) #2994
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reason for the pull request
I noticed an odd behavior with an app I was working on that uses Apollo. When calling a mutation that takes an object, if I used the variable syntax to send the object, everything worked fine. However, if I passed the object directly as a parameter, I would get this error message:
TypeError: Cannot convert object to primitive value
, which was thrown deep in request body creation logic ofnode-fetch
.This works:
This does not:
What does this have to do with
graphql-js
?As I debugged the two scenarios above, the one difference I discovered is that the
thing
object passed into my resolver using the object value syntax didn't have a prototype, whereas thething
object sent as a variable did. 🤔 "That's odd," I thought. So I dug further to see how that object was being created. This led me to this project, andvalueFromAST()
. If the object was passed as a variable, it returned a normal object. If, however, the object was an ObjectValue, a new object was created usingObject.create(null)
, and then the properties were copied to this object.Sure enough
Object.create(null)
creates an object without a prototype, which can lead to some interesting behavior, as documented here.This PR changes this use of
Object.create(null)
to{}
, giving the coerced object a full object prototype, and good behavior as it's being used elsewhere in my application.