-
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
Should parseLiteral
throw a TypeError when provided an invalid value?
#1821
Comments
parseLiteral
throw TypeError? parseLiteral
throw a TypeError when provided an invalid value?
@danielrearden Thanks for bringing this up and especially for deep research 👍
As I understand, it's like that because initially, it was problematic to parse SDL with complex default values. So returning
Yes, it would be great. One note though: |
This enhancement is tracked in #1827 |
When calling a
GraphQLScalarType
'sparseLiteral
orparseValue
methods, those methods can either returnundefined
or throw an error -- in either case, the value is considered invalid and an error is included in the response. However, returningundefined
results in a generic error message about the type being invalid, while throwing a TypeError allows us to provide additional details as to why the provided value was invalid.For all built-in scalars,
parseValue
throws a TypeError when the value is invalid, whileparseLiteral
always returns undefined. This results in inconsistent messaging when validating literal inputs compared to variables. For example:compared with
Anywhere
parseLiteral
is actually called, it's already wrapped with a try/catch and any error is handled appropriately -- namely here and here.I know this is fairly trivial, but is there any reason to avoid throwing a TypeError inside
parseLiteral
when the value is invalid? Even ifparseLiteral
andparseValue
can't share the exact same logic, it might make sense to reconcile them, at least in that regard. Would you accept a PR to that affect?The text was updated successfully, but these errors were encountered: