-
-
Notifications
You must be signed in to change notification settings - Fork 536
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(pydantic): Check for unused strawberry.auto fields when converting BaseModel #1551
fix(pydantic): Check for unused strawberry.auto fields when converting BaseModel #1551
Conversation
…g from pydantic models.
@@ -446,7 +446,7 @@ class BranchAType(BaseType): | |||
class BranchBType(BaseType): | |||
pass | |||
|
|||
@strawberry.experimental.pydantic.type(User, fields=["age", "interface_field"]) | |||
@strawberry.experimental.pydantic.type(User, fields=["interface_field"]) | |||
class UserType: |
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.
mistakes caught in our tests :D
Codecov Report
@@ Coverage Diff @@
## main #1551 +/- ##
=======================================
Coverage 98.11% 98.11%
=======================================
Files 130 130
Lines 4447 4461 +14
Branches 755 758 +3
=======================================
+ Hits 4363 4377 +14
Misses 44 44
Partials 40 40 |
Thanks for adding the Here's a preview of the changelog: This release checks for AutoFieldsNotInBaseModelError when converting from pydantic models. class User(BaseModel):
age: int
@strawberry.experimental.pydantic.type(User)
class UserType:
age: strawberry.auto
password: strawberry.auto Previously no errors would be raised, and the password field would not appear on graphql schema. Here's the preview release card for twitter: Here's the tweet text:
|
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.
Looks great! I've only added a minor comment 😊
|
||
|
||
def test_cannot_convert_pydantic_type_to_strawberry_property_auto(): | ||
# auto inferring type of a property is not supported |
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.
should this be supported in future? 🤔
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.
that would be nice, though idk how complicated it would be. i think we could isinstance(method, property) to check if its a property. and then we could extract the return type from the code.
Co-authored-by: Patrick Arminio <[email protected]>
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.
<3
Description
Checks for AutoFieldsNotInBaseModelError when converting from pydantic models.
It is raised when strawberry.auto is used, but the pydantic model does not have
the particular field defined.
Previously no errors would be raised, and the password field would not appear on graphql schema.
Such mistakes could be common during refactoring. E.g. if you rename age to user_age on the basemodel.
but you forget to change age on the UserType. Then the age field will disappear on the graphql schema
Now, AutoFieldsNotInBaseModelError is raised.
(maybe u want to warn instead of raising error? not sure)
Types of Changes
Issues Fixed or Closed by This PR
Checklist