-
-
Notifications
You must be signed in to change notification settings - Fork 30
Improve support for type inheritance from other mapped types #253
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
Open
Ckk3
wants to merge
9
commits into
strawberry-graphql:main
Choose a base branch
from
Ckk3:issue-249
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
9 commits
Select commit
Hold shift + click to select a range
73822a6
add test_inheritance_table
Ckk3 24d4e21
fix inheritance with mapped types
Ckk3 70af631
remove mapped_collum to work with sqlalchemy 1.4
Ckk3 766a3e9
fix schema expected
Ckk3 7797624
adding new fixes to python 3.8 and 3.9
Ckk3 cf3aae9
Update docs and release
Ckk3 28a579b
Update RELEASE.md
Ckk3 6ca26c6
Update README.md
Ckk3 0aa5033
Update src/strawberry_sqlalchemy_mapper/mapper.py
Ckk3 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 hidden or 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 hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
Release type: patch | ||
|
||
This release improves how types inherit fields from other mapped types using `@mapper.type(...)`. | ||
You can now safely inherit from another mapped type, and the resulting GraphQL type will include all expected fields with predictable conflict resolution. | ||
|
||
Some examples: | ||
|
||
- Basic Inheritance: | ||
|
||
```python | ||
@mapper.type(ModelA) | ||
class ApiA: | ||
pass | ||
|
||
|
||
@mapper.type(ModelB) | ||
class ApiB(ApiA): | ||
# ApiB inherits all fields declared in ApiA | ||
pass | ||
``` | ||
|
||
|
||
- The `__exclude__` option continues working: | ||
|
||
```python | ||
@mapper.type(ModelA) | ||
class ApiA: | ||
__exclude__ = ["relationshipB_id"] | ||
|
||
|
||
@mapper.type(ModelB) | ||
class ApiB(ApiA): | ||
# ApiB will have all fields declared in ApiA, except "relationshipB_id" | ||
pass | ||
``` | ||
|
||
- If two SQLAlchemy models define fields with the same name, the field from the model inside `.type(...)` takes precedence: | ||
|
||
```python | ||
class ModelA(base): | ||
__tablename__ = "a" | ||
|
||
id = Column(String, primary_key=True) | ||
example_field = Column(String(50)) | ||
|
||
|
||
class ModelB(base): | ||
__tablename__ = "b" | ||
|
||
id = Column(String, primary_key=True) | ||
example_field = Column(Integer, autoincrement=True) | ||
|
||
|
||
@mapper.type(ModelA) | ||
class ApiA: | ||
# example_field will be a String | ||
pass | ||
|
||
|
||
@mapper.type(ModelB) | ||
class ApiB(ApiA): | ||
# example_field will be taken from ModelB and will be an Integer | ||
pass | ||
``` | ||
|
||
|
||
- If a field is explicitly declared in the mapped type, it will override any inherited or model-based definition: | ||
|
||
```python | ||
class ModelA(base): | ||
__tablename__ = "a" | ||
|
||
id = Column(String, primary_key=True) | ||
example_field = Column(String(50)) | ||
|
||
|
||
class ModelB(base): | ||
__tablename__ = "b" | ||
|
||
id = Column(String, primary_key=True) | ||
example_field = Column(Integer, autoincrement=True) | ||
|
||
|
||
@mapper.type(ModelA) | ||
class ApiA: | ||
pass | ||
|
||
|
||
@mapper.type(ModelB) | ||
class ApiB(ApiA): | ||
# example_field will be a Float | ||
example_field: float = strawberry.field(name="exampleField") | ||
``` |
This file contains hidden or 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.
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.
question: what is the exact difference between them? In theory they should be handling that the same
One thing that I know is that in previous versions,
__annotations__
could not exist in the class if that class didn't have any annotations in it, something which I had to workaround on strawberry-django.Is that it or something else?
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.
Hi @bellini666, sorry for the delay!
The problem isn’t directly related to
__annotations__
existance, but to inconsistencies with inherited classes and this annotations.In Python 3.10 and newer, the
__annotations__
no longer includes the annotations fromoriginal_type
(base class in this case), which caused some resolvers or type hints to be missing in the final class. So I had to add extra logic to extract those annotations manually.I didn’t investigate this deeply, but I suspect it only happens because we’re using a decorator, which might interfere with how Python handles class inheritance and type resolution at that point.
On the other hand, in Python 3.8 and 3.9, the inherited annotations are already present in the subclass’s
__annotations__
. So thats why I only add missing keys, since its was raising a error when only usingupdate()
.Let me know if you see any better idea to fix this, this one was the most stable solution I found to make it work consistently across versions.