-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Scalar coercion cleanup #1414
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
Scalar coercion cleanup #1414
Conversation
9f803fe
to
e5f4c44
Compare
}); | ||
|
||
it('serializes output as Boolean', () => { | ||
expect(GraphQLBoolean.serialize('string')).to.equal(true); | ||
expect(GraphQLBoolean.serialize('false')).to.equal(true); | ||
expect(GraphQLBoolean.serialize('')).to.equal(false); |
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.
Converting non-empty strings to true
is confusing because GraphQLString
coerce true
into "true"
. Also, spec doesn't mention this coercion:
http://facebook.github.io/graphql/June2018/#sec-Boolean
@@ -91,7 +91,7 @@ describe('Execute: Handles execution with a complex schema', () => { | |||
function article(id) { | |||
return { | |||
id, | |||
isPublished: 'true', |
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.
one more example why current coercion rules looks strange and confusing:
"true" => true
"false" => true
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 solid, I'm happy to merge once the logic is a little bit cleaner.
@@ -171,8 +185,8 @@ describe('Type System: Scalar coercion', () => { | |||
'ID cannot represent value: true', | |||
); | |||
|
|||
expect(() => GraphQLID.serialize(-1.1)).to.throw( | |||
'ID cannot represent value: -1.1', |
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.
Oh that's a great change: making the test clear the reason it fails is floating-point coercion not negative-number coercion.
src/type/scalars.js
Outdated
throw new TypeError( | ||
`Int cannot represent an array value: ${inspect(value)}`, | ||
); | ||
let num = 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.
I feel like this logic could be simplified. It's really strange to me to have an early-return in an else block:
if (typeof value === 'boolean') {
return value ? 1 : 0;
}
const num = (typeof value === 'string' && value !== '') ?
Number(value) :
value;
if (!isInteger(num)) {
src/type/scalars.js
Outdated
throw new TypeError( | ||
`Float cannot represent an array value: ${inspect(value)}`, | ||
); | ||
let num = 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.
same idea here
e5f4c44
to
7f631d2
Compare
@mjmahone Thanks for review 👍 Fixed 🔧 |
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 is great! Thank you!
Idea is to make coercion code more strict and explicit.