Int scalar type coercion

Hello guys!

First of all thanks for all the great effort you’re putting into RedwoodJS, for a seasoned Rails developer like me this checks a lot of boxes and feels really good.

I’m building a small app to get familiar with the framework where I have a Product model with a name (string) a price_in_cents (integer) fields.

I’ve scaffolded the UI and tried to add a new product, but when I save it I get a GraphQL error: Expected type Int. Int cannot represent non-integer value: "100". I’m assuming this happens because the <input> sends the value as a string, while my model defines that field as an Int.

Is that the case or am I missing something? I’m new to GraphQL, so that might be the case :slight_smile:

What’s the best way (and where’s the best place) to coerce the value to an integer so that it doesn’t fail validation?

This similar example works around the issue by modeling the price as a String, which honestly just doesn’t feel right :confused:

Bonus question: would it be possible for the scaffold generator to generate an <input type="number" step="1"> to map Int fields? It won’t fix the previous issue, but it would improve the experience and minimize user errors.

Thanks!

I’d try to output your form data to console before submission like in the Redwood Tutorial for Saving data

That could also enable you to cast it to Int before submitting.

1 Like

Good point. Massaging data inside onSubmit does indeed work :slight_smile:

Still wondering if that’s the best practice or if there are alternatives…

Thanks for the idea, @janmechtel!

Hi all! This is an important issue we’re hoping to improve ('cause a lot of people using forms are going to run into this pattern).

I’ve added a specific topic with an example here.

And here’s a GitHub Issue about this topic that will hopefully become a working plan of attack to improve things.

2 Likes