From 7a06ff48126f23946ea5a19ebbe738108ab9f852 Mon Sep 17 00:00:00 2001 From: stygmate Date: Tue, 25 Jun 2024 13:14:16 +0200 Subject: [PATCH 1/5] Add node polymorphism tests and related schema --- tests/node_polymorphism/__init__.py | 0 tests/node_polymorphism/models.py | 14 ++++++ tests/node_polymorphism/schema.py | 36 ++++++++++++++++ tests/node_polymorphism/test_optimizer.py | 52 +++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 tests/node_polymorphism/__init__.py create mode 100644 tests/node_polymorphism/models.py create mode 100644 tests/node_polymorphism/schema.py create mode 100644 tests/node_polymorphism/test_optimizer.py diff --git a/tests/node_polymorphism/__init__.py b/tests/node_polymorphism/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/node_polymorphism/models.py b/tests/node_polymorphism/models.py new file mode 100644 index 00000000..6d6b1378 --- /dev/null +++ b/tests/node_polymorphism/models.py @@ -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) diff --git a/tests/node_polymorphism/schema.py b/tests/node_polymorphism/schema.py new file mode 100644 index 00000000..da389eb4 --- /dev/null +++ b/tests/node_polymorphism/schema.py @@ -0,0 +1,36 @@ +from typing import List + +import strawberry +from strawberry.relay import ListConnection + +import strawberry_django +from strawberry_django.optimizer import DjangoOptimizerExtension + +from .models import ArtProject, Project, ResearchProject + + +@strawberry_django.interface(Project) +class ProjectType(strawberry.relay.Node): + 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: ListConnection[ProjectType] = strawberry_django.connection() + + +schema = strawberry.Schema( + query=Query, + types=[ArtProjectType, ResearchProjectType], + extensions=[DjangoOptimizerExtension], +) diff --git a/tests/node_polymorphism/test_optimizer.py b/tests/node_polymorphism/test_optimizer.py new file mode 100644 index 00000000..59d4c1d5 --- /dev/null +++ b/tests/node_polymorphism/test_optimizer.py @@ -0,0 +1,52 @@ +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 { + edges { + node { + __typename + topic + ... on ArtProjectType { + artist + } + ... on ResearchProjectType { + supervisor + } + } + } + } + } + """ + + result = schema.execute_sync(query) + assert not result.errors + assert result.data == { + 'projects': { + 'edges': [ + { + 'node': { + '__typename': 'ArtProjectType', + 'topic': ap.topic, + 'artist': ap.artist + } + }, + { + 'node': { + '__typename': 'ResearchProjectType', + 'topic': rp.topic, + 'supervisor': rp.supervisor + } + } + ] + } + } From a42386666a3a5d0d1f46e8642c75092126f437e6 Mon Sep 17 00:00:00 2001 From: stygmate Date: Tue, 25 Jun 2024 13:15:21 +0200 Subject: [PATCH 2/5] Update asserts in optimizer.py --- strawberry_django/optimizer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strawberry_django/optimizer.py b/strawberry_django/optimizer.py index 360b5908..d8048e5a 100644 --- a/strawberry_django/optimizer.py +++ b/strawberry_django/optimizer.py @@ -843,7 +843,7 @@ def _get_model_hints_from_connection( schema, get_object_definition(e_type, strict=True), ) - assert isinstance(e_gql_definition, GraphQLObjectType) + assert isinstance(e_gql_definition, GraphQLObjectType | GraphQLInterfaceType) e_info = _generate_selection_resolve_info( info, edges, @@ -856,7 +856,7 @@ def _get_model_hints_from_connection( continue n_gql_definition = _get_gql_definition(schema, n_definition) - assert isinstance(n_gql_definition, GraphQLObjectType) + assert isinstance(n_gql_definition, GraphQLObjectType | GraphQLInterfaceType) n_info = _generate_selection_resolve_info( info, nodes, From fb40e9f921b091575c88433e8d44eeb796751eec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Jun 2024 19:10:01 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strawberry_django/optimizer.py | 4 +++- tests/node_polymorphism/schema.py | 1 - tests/node_polymorphism/test_optimizer.py | 22 +++++++++++----------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/strawberry_django/optimizer.py b/strawberry_django/optimizer.py index d8048e5a..9df73da9 100644 --- a/strawberry_django/optimizer.py +++ b/strawberry_django/optimizer.py @@ -856,7 +856,9 @@ def _get_model_hints_from_connection( continue n_gql_definition = _get_gql_definition(schema, n_definition) - assert isinstance(n_gql_definition, GraphQLObjectType | GraphQLInterfaceType) + assert isinstance( + n_gql_definition, GraphQLObjectType | GraphQLInterfaceType + ) n_info = _generate_selection_resolve_info( info, nodes, diff --git a/tests/node_polymorphism/schema.py b/tests/node_polymorphism/schema.py index da389eb4..462c4061 100644 --- a/tests/node_polymorphism/schema.py +++ b/tests/node_polymorphism/schema.py @@ -1,4 +1,3 @@ -from typing import List import strawberry from strawberry.relay import ListConnection diff --git a/tests/node_polymorphism/test_optimizer.py b/tests/node_polymorphism/test_optimizer.py index 59d4c1d5..d53806ac 100644 --- a/tests/node_polymorphism/test_optimizer.py +++ b/tests/node_polymorphism/test_optimizer.py @@ -31,22 +31,22 @@ def test_polymorphic_interface_query(): result = schema.execute_sync(query) assert not result.errors assert result.data == { - 'projects': { - 'edges': [ + "projects": { + "edges": [ { - 'node': { - '__typename': 'ArtProjectType', - 'topic': ap.topic, - 'artist': ap.artist + "node": { + "__typename": "ArtProjectType", + "topic": ap.topic, + "artist": ap.artist, } }, { - 'node': { - '__typename': 'ResearchProjectType', - 'topic': rp.topic, - 'supervisor': rp.supervisor + "node": { + "__typename": "ResearchProjectType", + "topic": rp.topic, + "supervisor": rp.supervisor, } - } + }, ] } } From 41149b8c320c3b778088dd086207dd3fcdd5cb15 Mon Sep 17 00:00:00 2001 From: Thiago Bellini Ribeiro Date: Sun, 30 Jun 2024 10:58:40 -0300 Subject: [PATCH 4/5] refactor: Use tuples for isinstance for python 3.8/3.9 compatibility --- strawberry_django/optimizer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/strawberry_django/optimizer.py b/strawberry_django/optimizer.py index 9df73da9..bd2ab744 100644 --- a/strawberry_django/optimizer.py +++ b/strawberry_django/optimizer.py @@ -843,7 +843,7 @@ def _get_model_hints_from_connection( schema, get_object_definition(e_type, strict=True), ) - assert isinstance(e_gql_definition, GraphQLObjectType | GraphQLInterfaceType) + assert isinstance(e_gql_definition, (GraphQLObjectType, GraphQLInterfaceType)) e_info = _generate_selection_resolve_info( info, edges, @@ -857,7 +857,8 @@ def _get_model_hints_from_connection( n_gql_definition = _get_gql_definition(schema, n_definition) assert isinstance( - n_gql_definition, GraphQLObjectType | GraphQLInterfaceType + n_gql_definition, + (GraphQLObjectType, GraphQLInterfaceType), ) n_info = _generate_selection_resolve_info( info, From b9d580a59f9e93f112943ab7abcc616dd94f80ba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:59:09 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/node_polymorphism/schema.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/node_polymorphism/schema.py b/tests/node_polymorphism/schema.py index 462c4061..c9e96d17 100644 --- a/tests/node_polymorphism/schema.py +++ b/tests/node_polymorphism/schema.py @@ -1,4 +1,3 @@ - import strawberry from strawberry.relay import ListConnection