Skip to content

Feat: Adding the option of importing interfaces from strings #423

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion grapple/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.apps import apps
from django.core.exceptions import FieldDoesNotExist
from django.db import models
from django.utils.module_loading import import_string
from graphene_django.types import DjangoObjectType
from wagtail.blocks import StructValue, stream_block
from wagtail.contrib.settings.models import BaseGenericSetting, BaseSiteSetting
Expand Down Expand Up @@ -268,7 +269,13 @@ class StubMeta:
model = stub_model

# Gather any interfaces, and discard None values
interfaces = {interface, *getattr(cls, "graphql_interfaces", ())}
interface_classes = list(getattr(cls, "graphql_interfaces", ()))
for i, interface_class in enumerate(interface_classes):
if isinstance(interface_class, str):
interface_classes[i] = import_string(interface_class)
interface_classes = tuple(interface_classes)

interfaces = {interface, *interface_classes}
interfaces.discard(None)

type_meta = {
Expand Down
14 changes: 11 additions & 3 deletions tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,29 @@ def test_schema_for_page_with_graphql_interface(self):
results = self.introspect_schema_by_type("AuthorPage")
self.assertListEqual(
sorted(results["data"]["__type"]["interfaces"], key=lambda x: x["name"]),
[{"name": "AdditionalInterface"}, {"name": "PageInterface"}],
[
{"name": "AdditionalInterface"},
{"name": "AlternativeInterface"},
{"name": "PageInterface"},
],
)

def test_schema_for_snippet_with_graphql_interface(self):
results = self.introspect_schema_by_type("Advert")
self.assertListEqual(
sorted(results["data"]["__type"]["interfaces"], key=lambda x: x["name"]),
[{"name": "AdditionalInterface"}, {"name": "SnippetInterface"}],
[
{"name": "AdditionalInterface"},
{"name": "AlternativeInterface"},
{"name": "SnippetInterface"},
],
)

def test_schema_for_django_model_with_graphql_interfaces(self):
results = self.introspect_schema_by_type("SimpleModel")
self.assertListEqual(
sorted(results["data"]["__type"]["interfaces"], key=lambda x: x["name"]),
[{"name": "AdditionalInterface"}],
[{"name": "AdditionalInterface"}, {"name": "AlternativeInterface"}],
)


Expand Down
4 changes: 4 additions & 0 deletions tests/testapp/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class AdditionalInterface(graphene.Interface):
additional_text = graphene.String()


class AlternativeInterface(graphene.Interface):
alternative_text = graphene.String()


class CustomPageInterface(PageInterface):
custom_text = graphene.String()

Expand Down
15 changes: 12 additions & 3 deletions tests/testapp/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@

@register_singular_query_field("simpleModel")
class SimpleModel(models.Model):
graphql_interfaces = (AdditionalInterface,)
graphql_interfaces = (
AdditionalInterface,
"testapp.interfaces.AlternativeInterface",
)


def custom_middleware_one(next, root, info, **args):
Expand Down Expand Up @@ -83,7 +86,10 @@ class AuthorPage(Page):
content_panels = Page.content_panels + [FieldPanel("name")]

graphql_fields = [GraphQLString("name")]
graphql_interfaces = (AdditionalInterface,)
graphql_interfaces = (
AdditionalInterface,
"testapp.interfaces.AlternativeInterface",
)


class BlogPageTag(TaggedItemBase):
Expand Down Expand Up @@ -264,7 +270,10 @@ class Advert(models.Model):
GraphQLString("string_rich_text", source="rich_text"),
GraphQLString("extra_rich_text", deprecation_reason="Use rich_text instead"),
]
graphql_interfaces = (AdditionalInterface,)
graphql_interfaces = (
AdditionalInterface,
"testapp.interfaces.AlternativeInterface",
)

def __str__(self):
return self.text
Expand Down