-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
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 value 0000-00-00 00:00:00
.
The mysql2
DB adapter automatically converts the db value to a JS date via the Date
constructor, which for the zero'd value above gives an Invalid Date
.
I was creating a custom date type with the serialize
function as follows:
serialize: function(value : Date) {
if (isNaN(value.getTime()) {
return null
}
return customDateFormatter(value)
},
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
const serializedResult = returnType.serialize(result); | |
if (isNullish(serializedResult)) { | |
throw new Error( | |
`Expected a value of type "${String(returnType)}" but ` + | |
`received: ${String(result)}` | |
); | |
} |
In execute.js
completeValue
:
graphql-js/src/execution/execute.js
Lines 907 to 910 in e236ca2
// If result value is null-ish (null, undefined, or NaN) then return null. | |
if (isNullish(result)) { | |
return null; | |
} |
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 if serialize
returns null
/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 return null
?