strawberry
strawberry copied to clipboard
Add oneOf Input Objects directive (#2421)
Description
Types of Changes
- [ ] Core
- [ ] Bugfix
- [ ] New feature
- [ ] Enhancement/optimization
- [ ] Documentation
Issues Fixed or Closed by This PR
- #2421
Checklist
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the CONTRIBUTING document.
- [ ] I have added tests to cover my changes.
- [ ] I have tested the changes and verified that they work and don't break anything (as well as I can manage).
Codecov Report
Merging #2560 (ab47a3b) into main (0cd6d9d) will increase coverage by
0.04%. The diff coverage is100.00%.
Additional details and impacted files
@@ Coverage Diff @@
## main #2560 +/- ##
==========================================
+ Coverage 96.25% 96.30% +0.04%
==========================================
Files 183 184 +1
Lines 7483 7574 +91
Branches 1374 1395 +21
==========================================
+ Hits 7203 7294 +91
- Misses 178 179 +1
+ Partials 102 101 -1
Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:
This release adds the OneOf directive to indicate that the input type is a oneOf Input Object, where exactly one of the input fields must be provided as input,
or otherwise the server returns a validation error.
Instead of defining a field for each property which a user can be retrieved we can now have a single field user that accepts all properties via a UserByInput OneOf input object, knowing that only one of the properties (either the ID or the email) can be provided:
import strawberry
from strawberry.schema_directives import OneOf
@strawberry.input(directives=[OneOf])
class UserByInput:
id: strawberry.ID
name: str
@strawberry.type
class Query:
@strawberry.field
def user(self, by: UserByInput) -> User:
...
Here's the preview release card for twitter:

Here's the tweet text:
π Release (next) is out! Thanks to @esterbeltrami for the PR π
Get it here π https://github.com/strawberry-graphql/strawberry/releases/tag/(next)
What is the status of this PR? Is there any reason it was not considered/reviewed for over a year?
@colonelpanic8 adding the directives unfortunately isn't enough to make this work, we also need a way to validate that the input conforms to the spec π
Sure. But wouldn't this represent a first step? Are you hoping for a pr that does both?
Itβs more that having the directive is going to be misleading, since it wonβt do any validation βΊοΈ
Iβll try a POC for this soon though
This was done in https://github.com/strawberry-graphql/strawberry/pull/3429 with runtime support π