How to define JSON custom scalar type in graphql which can be nullable? #3981
Replies: 2 comments
-
|
The v16 change requires |
Beta Was this translation helpful? Give feedback.
-
|
Nullability should be modeled on the field type, not inside the scalar. For example: type Example {
metadata: JSON
requiredMetadata: JSON!
}
So the scalar implementation should focus on valid JSON values when it is called: const JSONScalar = new GraphQLScalarType({
name: 'JSON',
serialize(value) {
if (value === null || value === undefined) {
throw new TypeError('JSON scalar cannot serialize null directly')
}
return value
},
parseValue(value) {
return value
},
})If In short: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
#3532 (comment)
I want to define a JSON custom scalar type which can be nullable.
after graphql 16,
serialize()can not return null, so I'm getting GRAPHQL error {"errors":[{"message":"Expected JsonType.serialize("null") to return non-nullable value, returned: null" error where JsonType is my customized scalar.Wondering what's the recommended way to do this?
Beta Was this translation helpful? Give feedback.
All reactions