-
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
Add resolver validation, check if value is a function #669
Conversation
@@ -506,6 +506,11 @@ function defineFieldMap<TSource, TContext>( | |||
`${type.name}.${fieldName} field type must be Output Type but ` + | |||
`got: ${String(field.type)}.` | |||
); | |||
invariant( | |||
isValidResolver(field.resolve), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could probably just inline this as !field.resolve || typeof field.resolve === 'function'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"!field.resolve" means we will accept any "falsey" value such as 0 or the empty string. Is that what we want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, that it would let in falsey values. We could instead use field.resolve == null || typeof field.resolve === 'function'
to be more specific
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that, but the linter treats == as an error. I can update the linter to allow it if you feel strongly that == is more expressive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably enable == null
in the linter. I think eslint now has the ability to whitelist that particular kind of usage. It's equivalent to the longer form.
@@ -506,6 +506,11 @@ function defineFieldMap<TSource, TContext>( | |||
`${type.name}.${fieldName} field type must be Output Type but ` + | |||
`got: ${String(field.type)}.` | |||
); | |||
invariant( | |||
isValidResolver(field.resolve), | |||
`${type.name}.${fieldName} field resolver must be function or null/` + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about field resolver must be a function if provided, but got:
btw - this looks good - you should merge once you've got something you think is best |
If a field's resolver is set to a constant value like "{}" we get a runtime error, "resolveFn is not a function". We can easily detect this case when we validate fields during schema creation. This PR adds a new validation rule to check that the resolve definition is a function.