strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Add oneOf Input Objects directive (#2421)

Open estyxx opened this issue 2 years ago β€’ 6 comments

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).

estyxx avatar Feb 18 '23 13:02 estyxx

Codecov Report

Merging #2560 (ab47a3b) into main (0cd6d9d) will increase coverage by 0.04%. The diff coverage is 100.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     

codecov[bot] avatar Feb 18 '23 13:02 codecov[bot]

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)

botberry avatar Feb 18 '23 13:02 botberry

What is the status of this PR? Is there any reason it was not considered/reviewed for over a year?

colonelpanic8 avatar Mar 31 '24 02:03 colonelpanic8

@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 😊

patrick91 avatar Mar 31 '24 05:03 patrick91

Sure. But wouldn't this represent a first step? Are you hoping for a pr that does both?

colonelpanic8 avatar Mar 31 '24 05:03 colonelpanic8

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

patrick91 avatar Mar 31 '24 07:03 patrick91

This was done in https://github.com/strawberry-graphql/strawberry/pull/3429 with runtime support 😊

patrick91 avatar May 25 '24 14:05 patrick91