-
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
returning null for a custom scalar's serialize #1057
Comments
Reading through the graph-ql spec - in particular this section: https://facebook.github.io/graphql/October2016/#sec-Value-Completion
It seems like the design of graphql-js goes against the spec in this instance. |
It's a bit fuzzy though, because this section: https://facebook.github.io/graphql/October2016/#sec-Scalars
The first sentence doesn't really mention how to handle nulls, and when taken in conjunction with the above extract on value completion, it seems like throwing an error is optional. |
This changes the check for null/undefined to a check for undefined to determine if scalar serialization was successful or not, allowing `null` to be returned from serialize() without indicating error. This is potentially breaking for any existing custom scalar which returned `null` from `serialize()` to indicate failure. To account for this change, it should either throw an error or return `undefined`. Fixes #1057 Closes #1066
This changes the check for null/undefined to a check for undefined to determine if scalar serialization was successful or not, allowing `null` to be returned from serialize() without indicating error. This is potentially breaking for any existing custom scalar which returned `null` from `serialize()` to indicate failure. To account for this change, it should either throw an error or return `undefined`. Fixes #1057 Closes #1066
I'm trying to build some custom types to create reusable type/value checking within my API.
The problem I've got is that if there's a problem with the serialization, there's no way to silently ignore the value.
For example, in mysql, a non-null
DATETIME
that's ignored at insert time is assigned the value0000-00-00 00:00:00
.The
mysql2
DB adapter automatically converts the db value to a JS date via theDate
constructor, which for the zero'd value above gives anInvalid Date
.I was creating a custom date type with the
serialize
function as follows:The problem is that if you return null from the
serialize
function, graphql will automatically throw an error on your behalf (graphql-js/src/execution/execute.js
Lines 1015 to 1021 in e236ca2
In
execute.js
completeValue
:graphql-js/src/execution/execute.js
Lines 907 to 910 in e236ca2
The internals will happily silently return null (and not call
serialize
) if the value that comes into the function is null, but there is no way to stop it throwing ifserialize
returnsnull
/undefined
.Considering that I can happily throw a handled exception from within the
serialize
function, how come you chose to also automatically throw one for me?I can think of a lot of use cases where you might want to silently ignore the fact that
serialize
returns null.Is it possible to get a way to make graphql-js allow a
serialize
function to returnnull
?The text was updated successfully, but these errors were encountered: