-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Extract interface definition in optimizer to fix django-polymorp…
…hic (#556) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Thiago Bellini Ribeiro <[email protected]>
- Loading branch information
1 parent
890b109
commit bfc598a
Showing
8 changed files
with
146 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,5 +113,6 @@ | |
[ | ||
"tests", | ||
"tests.projects", | ||
"tests.polymorphism", | ||
], | ||
) |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.db import models | ||
from polymorphic.models import PolymorphicModel | ||
|
||
|
||
class Project(PolymorphicModel): | ||
topic = models.CharField(max_length=30) | ||
|
||
|
||
class ArtProject(Project): | ||
artist = models.CharField(max_length=30) | ||
|
||
|
||
class ResearchProject(Project): | ||
supervisor = models.CharField(max_length=30) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import List | ||
|
||
import strawberry | ||
|
||
import strawberry_django | ||
from strawberry_django.optimizer import DjangoOptimizerExtension | ||
|
||
from .models import ArtProject, Project, ResearchProject | ||
|
||
|
||
@strawberry_django.interface(Project) | ||
class ProjectType: | ||
topic: strawberry.auto | ||
|
||
|
||
@strawberry_django.type(ArtProject) | ||
class ArtProjectType(ProjectType): | ||
artist: strawberry.auto | ||
|
||
|
||
@strawberry_django.type(ResearchProject) | ||
class ResearchProjectType(ProjectType): | ||
supervisor: strawberry.auto | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
projects: List[ProjectType] = strawberry_django.field() | ||
|
||
|
||
schema = strawberry.Schema( | ||
query=Query, | ||
types=[ArtProjectType, ResearchProjectType], | ||
extensions=[DjangoOptimizerExtension], | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pytest | ||
|
||
from .models import ArtProject, ResearchProject | ||
from .schema import schema | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
def test_polymorphic_interface_query(): | ||
ap = ArtProject.objects.create(topic="Art", artist="Artist") | ||
rp = ResearchProject.objects.create(topic="Research", supervisor="Supervisor") | ||
|
||
query = """\ | ||
query { | ||
projects { | ||
__typename | ||
topic | ||
... on ArtProjectType { | ||
artist | ||
} | ||
... on ResearchProjectType { | ||
supervisor | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = schema.execute_sync(query) | ||
assert not result.errors | ||
assert result.data == { | ||
"projects": [ | ||
{"__typename": "ArtProjectType", "topic": ap.topic, "artist": ap.artist}, | ||
{ | ||
"__typename": "ResearchProjectType", | ||
"topic": rp.topic, | ||
"supervisor": rp.supervisor, | ||
}, | ||
] | ||
} |