-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix: allow nullable input objects and arrays #67
Open
peterc1731
wants to merge
2
commits into
mercurius-js:main
Choose a base branch
from
peterc1731:fix/nullability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm wondering if it's better to define nullability on the usage rather than the definition itself so we have a validation schema that fully aligns with the GraphQL schema, WDYT?
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.
Without this we are technically already still defining nullability on the definition, just implicitly setting it to
false
, so even if the validation schema containsnullable: true
at the point of usage it will fail validation when we send null because the definition itself is non-nullable.I'm not sure it would be possible to allow objects to be null without this part, unless you have any other ideas how we could do it?
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 think we could maybe do it with an
anyOf
in this function: https://github.com/mercurius-js/validation/pull/67/files#diff-ee1a5eaed8c16e887653f519f05ddf10825fb4acfe4320521cda3e701825046dR25That way we only set the nullability when it's applicable - wdyt?
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.
Ooh that is a good idea, I'll try it
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.
Sorry for the long wait getting back to you on this!
I've done some investigation and anyOf/oneOf in the spot you described does work to allow input objects to be null without needing this line, but it introduces a a few different issues that I think make this approach unviable.
Firstly, if the object that's being validated has failed on any of the nested fields, it will fail the anyOf check, as it doesn't exactly match the defined object or null, so we get back a lot of extra noise in the error response describing the real issue as nested underneath the failed anyOf check, which I think makes the error harder to understand.
Secondly, if there is a nullable object nested inside the input object, it seems to not be running validation on the fields inside that nested object. I wonder if a conditional schema check has some kind of depth limit? Or possibly I'm missing something with the implementation, but I've tried experimenting with a few similar approaches (like if/else/then) and haven't got this case to work correctly.
I'm all out of ideas for trying to avoid this line, but if you have any more suggestions please let me know and I'm happy to try them out!
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 comprehend the logic behind the removal of L114, but I'm not entirely clear on the significance of opting for oneOf over nullable.
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.
Apologies for my late reply on this! If the approach isn't viable, I think it's okay to go with your original approach as we are protected by the underlying GraphQL validation :) do you agree with this @mcollina ?
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.
Got some time this weekend, so gonna have a closer look at this one then :)
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.
Would love to eventually get this PR merged if you have any time to follow up @jonnydgreen or @mcollina !