-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Call 'toJSON' if present for ID and String serialize #1520
Conversation
@alexmcmillan I don't have access to MongoDB so I can't run: https://github.com/alexmcmillan/graphql-mongodb-query-fail-demo Can you please test it with attached package: |
@IvanGoncharov Can confirm the package you attached does resolve the problem. Thank you - I hope this gets merged soon! |
@alexmcmillan Thanks 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, thanks for fixing it!
src/type/scalars.js
Outdated
const result = | ||
value && typeof value.valueOf === 'function' ? value.valueOf() : value; | ||
// Support serializing objects with custom toJSON() or valueOf() functions - | ||
// a common way to represent an complex value which can be represented as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grammar nit: "a complex value" instead of "an complex value"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
Fixed
Prior to doing the bump to |
dabbf10
to
bf798d6
Compare
Agree, we definitely need to improve bus factor 👍
Yes 👍
AFAIK, it covered by semver:
My personal rule of thumb: if you add something new that clients explicitly depend on it means you need to do It also important for 3rd tools like So for me this change is not a bug fix but a new functionality especially since |
src/type/scalars.js
Outdated
// a common way to represent a complex value which can be represented as | ||
// a string (ex: MongoDB id objects). | ||
function serializeObject(value: mixed): mixed { | ||
if (typeof value === 'object' && value !== null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!= nulll
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> typeof undefined
'undefined'
@ruiaraujo So it will fail on the first check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, missed that.
src/type/scalars.js
Outdated
// a string (ex: MongoDB id objects). | ||
function serializeObject(value: mixed): mixed { | ||
if (typeof value === 'object' && value !== null) { | ||
if (typeof value.toJSON === 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is actually breaking - there may be objects which define both valueOf
and toJSON
which provide different values. If those objects previously worked correctly, this PR would cause them to work incorrectly.
If the goal is to also support objects which define toJSON
but do not define valueOf
then this could be easily solved by just moving this new addition to below the existing check for valueOf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that because:
> typeof ({}).valueOf
'function'
But I agree it breaking so I updated this PR to have this instead:
if (typeof value.valueOf === 'function') {
const valueOfResult = value.valueOf();
if (typeof valueOfResult !== 'object') {
return valueOfResult;
}
}
if (typeof value.toJSON === 'function') {
return value.toJSON();
}
I also changed tests to explicitly test precidence.
Can you please review new code and tests?
bf798d6
to
bd51729
Compare
I am having this problem also. Using "graphql": "^14.0.2"
|
I know its a bit of a late response but In regards to xabra's post try this: user: async(parent, args, ctx, info){
let user = await ctx.UserModel.findOne(args);
return { ...user._doc, _id: user._id.toString() };
} |
Well, I have no problems to query the _id field. But the same error happens while I try to populate sth. function findOne(id: string) {
const res = this.nodeModel.findOne({ id }).populate('parent');
return res;
} Finally I have this problem solved. The main issue is that As a workaround, set a transformer like this: NodeSchema.set('toJSON', {
transform(doc, ret) {
return JSON.stringify(ret);
},
}); |
Fixes #1518
class ObjectId
doesn't havevalueOf
onlytoString
andtoJSON
:https://github.com/mongodb/js-bson/blob/5acdebf7a63ed02d3bc1eb8481cdcb709fb5c886/lib/objectid.js#L213-L215
We can't use
toString
because:So I added support for
toJSON
.