Skip to content

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

Merged
merged 4 commits into from
Jul 20, 2018
Merged

Conversation

IvanGoncharov
Copy link
Member

Idea is to make coercion code more strict and explicit.

});

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);
Copy link
Member Author

@IvanGoncharov IvanGoncharov Jul 10, 2018

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',
Copy link
Member Author

@IvanGoncharov IvanGoncharov Jul 10, 2018

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

@IvanGoncharov IvanGoncharov added this to the v14.0.0 milestone Jul 10, 2018
@IvanGoncharov IvanGoncharov added the PR: breaking change 💥 implementation requires increase of "major" version number label Jul 13, 2018
Copy link
Contributor

@mjmahone mjmahone left a 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',
Copy link
Contributor

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.

throw new TypeError(
`Int cannot represent an array value: ${inspect(value)}`,
);
let num = value;
Copy link
Contributor

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)) {

throw new TypeError(
`Float cannot represent an array value: ${inspect(value)}`,
);
let num = value;
Copy link
Contributor

Choose a reason for hiding this comment

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

same idea here

@IvanGoncharov IvanGoncharov force-pushed the scalarCoercionCleanup branch from e5f4c44 to 7f631d2 Compare July 19, 2018 16:34
@IvanGoncharov
Copy link
Member Author

This looks solid, I'm happy to merge once the logic is a little bit cleaner.

@mjmahone Thanks for review 👍 Fixed 🔧

Copy link
Contributor

@mjmahone mjmahone left a 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!

@mjmahone mjmahone merged commit 3828c20 into graphql:master Jul 20, 2018
@IvanGoncharov IvanGoncharov deleted the scalarCoercionCleanup branch August 6, 2018 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed PR: breaking change 💥 implementation requires increase of "major" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants