diff --git a/docs/general-usage/decorators.rst b/docs/general-usage/decorators.rst index cf1f41b5..1423d783 100644 --- a/docs/general-usage/decorators.rst +++ b/docs/general-usage/decorators.rst @@ -433,12 +433,12 @@ To register additional interfaces for the block, add them with your block's ``gr from grapple.helpers import register_streamfield_block - class CustomInterface(graphene.Interface): + class MyInterface(graphene.Interface): text = graphene.String() @register_streamfield_block - class CustomInterfaceBlock(blocks.StructBlock): + class MyInterfaceBlock(blocks.StructBlock): text = blocks.TextBlock() - graphql_interfaces = (CustomInterface,) + graphql_interfaces = (MyInterface,) diff --git a/docs/general-usage/interfaces.rst b/docs/general-usage/interfaces.rst index 0b809a59..ea3d9d2d 100644 --- a/docs/general-usage/interfaces.rst +++ b/docs/general-usage/interfaces.rst @@ -118,10 +118,7 @@ Given the following example interface: .. code-block:: python # interfaces.py - from .interfaces import CustomInterface - - - class CustomInterface(graphene.Interface): + class MyInterface(graphene.Interface): custom_field = graphene.String() you could add it to your Page model like so: @@ -129,12 +126,13 @@ you could add it to your Page model like so: .. code-block:: python from wagtail.models import Page + from .interfaces import MyInterface class MyPage(Page): # ... - graphql_interfaces = (CustomInterface,) + graphql_interfaces = (MyInterface,) or any Django model: @@ -142,13 +140,13 @@ or any Django model: # models.py from django.db import models + from .interfaces import MyInterface class MyModel(models.Model): # ... - graphql_interfaces = (CustomInterface,) - + graphql_interfaces = (MyInterface,) or a ``StreamField`` block: @@ -156,11 +154,12 @@ or a ``StreamField`` block: # blocks.py from wagtail.core import blocks + from .interfaces import MyInterface class MyStructBlock(blocks.StructBlock): # ... - graphql_interfaces = (CustomInterface,) + graphql_interfaces = (MyInterface,) The provided interfaces will be added to the base interfaces for the model. diff --git a/tests/test_interfaces.py b/tests/test_interfaces.py index 6922e35e..7cce64b8 100644 --- a/tests/test_interfaces.py +++ b/tests/test_interfaces.py @@ -4,7 +4,7 @@ from django.test import override_settings, tag from test_grapple import BaseGrappleTestWithIntrospection -from testapp.factories import BlogPageFactory, CustomInterfaceBlockFactory +from testapp.factories import AdditionalInterfaceBlockFactory, BlogPageFactory from testapp.interfaces import CustomPageInterface from grapple.types.interfaces import PageInterface, get_page_interface @@ -21,7 +21,7 @@ def setUpTestData(cls): cls.blog_page = BlogPageFactory( body=[ - ("custom_interface_block", CustomInterfaceBlockFactory()), + ("additional_interface_block", AdditionalInterfaceBlockFactory()), ], parent=cls.home, ) @@ -41,15 +41,15 @@ def test_schema_with_default_page_interface(self): def test_get_page_interface_with_custom_page_interface(self): self.assertIs(get_page_interface(), CustomPageInterface) - def test_streamfield_block_with_custom_interface(self): + def test_streamfield_block_with_additional_interface(self): query = """ query($id: ID) { page(id: $id) { ... on BlogPage { body { blockType - ...on CustomInterface { - customText + ...on AdditionalInterface { + additionalText } } } @@ -60,40 +60,40 @@ def test_streamfield_block_with_custom_interface(self): body = results["data"]["page"]["body"] for block in body: - if block["blockType"] == "CustomInterfaceBlock": + if block["blockType"] == "AdditionalInterfaceBlock": self.assertRegex( - block["customText"], r"^Block with custom interface \d+$" + block["additionalText"], r"^Block with additional interface \d+$" ) return self.fail("Query by interface didn't match anything") - def test_schema_for_streamfield_block_with_custom_interface(self): - results = self.introspect_schema_by_type("CustomInterfaceBlock") + def test_schema_for_streamfield_block_with_additional_interface(self): + results = self.introspect_schema_by_type("AdditionalInterfaceBlock") self.assertListEqual( sorted(results["data"]["__type"]["interfaces"], key=lambda x: x["name"]), - [{"name": "CustomInterface"}, {"name": "StreamFieldInterface"}], + [{"name": "AdditionalInterface"}, {"name": "StreamFieldInterface"}], ) 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": "CustomInterface"}, {"name": "PageInterface"}], + [{"name": "AdditionalInterface"}, {"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": "CustomInterface"}], + [{"name": "AdditionalInterface"}], ) 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": "CustomInterface"}], + [{"name": "AdditionalInterface"}], ) diff --git a/tests/testapp/blocks.py b/tests/testapp/blocks.py index 0888e062..f5e606b4 100644 --- a/tests/testapp/blocks.py +++ b/tests/testapp/blocks.py @@ -24,7 +24,7 @@ GraphQLStreamfield, GraphQLString, ) -from testapp.interfaces import CustomInterface +from testapp.interfaces import AdditionalInterface if TYPE_CHECKING: @@ -294,17 +294,17 @@ def get_link_url( @register_streamfield_block -class CustomInterfaceBlock(blocks.StructBlock): +class AdditionalInterfaceBlock(blocks.StructBlock): """ - Specify a custom GraphQL interface for our block. + Specify an additional GraphQL interface for our block. """ - custom_text = blocks.TextBlock() + additional_text = blocks.TextBlock() graphql_fields = [ - GraphQLString("custom_text"), + GraphQLString("additional_text"), ] - graphql_interfaces = (CustomInterface,) + graphql_interfaces = (AdditionalInterface,) class StreamFieldBlock(blocks.StreamBlock): @@ -325,4 +325,4 @@ class StreamFieldBlock(blocks.StreamBlock): block_with_name = BlockWithName() advert = SnippetChooserBlock("testapp.Advert") person = SnippetChooserBlock("testapp.Person") - custom_interface_block = CustomInterfaceBlock() + additional_interface_block = AdditionalInterfaceBlock() diff --git a/tests/testapp/factories.py b/tests/testapp/factories.py index 35c46fde..5c66d7b7 100644 --- a/tests/testapp/factories.py +++ b/tests/testapp/factories.py @@ -9,7 +9,7 @@ from wagtail.contrib.redirects.models import Redirect from testapp.blocks import ( - CustomInterfaceBlock, + AdditionalInterfaceBlock, ImageGalleryBlock, ImageGalleryImage, ImageGalleryImages, @@ -88,11 +88,11 @@ class Meta: model = TextWithCallableBlock -class CustomInterfaceBlockFactory(wagtail_factories.StructBlockFactory): - custom_text = factory.Sequence(lambda n: f"Block with custom interface {n}") +class AdditionalInterfaceBlockFactory(wagtail_factories.StructBlockFactory): + additional_text = factory.Sequence(lambda n: f"Block with additional interface {n}") class Meta: - model = CustomInterfaceBlock + model = AdditionalInterfaceBlock class BlogPageRelatedLinkFactory(factory.django.DjangoModelFactory): diff --git a/tests/testapp/interfaces.py b/tests/testapp/interfaces.py index b473215d..08d45d0d 100644 --- a/tests/testapp/interfaces.py +++ b/tests/testapp/interfaces.py @@ -3,8 +3,8 @@ from grapple.types.interfaces import PageInterface -class CustomInterface(graphene.Interface): - custom_text = graphene.String() +class AdditionalInterface(graphene.Interface): + additional_text = graphene.String() class CustomPageInterface(PageInterface): diff --git a/tests/testapp/migrations/0003_alter_blogpage_body.py b/tests/testapp/migrations/0003_alter_blogpage_body.py new file mode 100644 index 00000000..86e0bbed --- /dev/null +++ b/tests/testapp/migrations/0003_alter_blogpage_body.py @@ -0,0 +1,126 @@ +# Generated by Django 5.0.9 on 2024-09-20 07:17 + +import wagtail.fields + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("testapp", "0002_create_homepage"), + ] + + operations = [ + migrations.AlterField( + model_name="blogpage", + name="body", + field=wagtail.fields.StreamField( + [ + ("heading", 0), + ("paragraph", 1), + ("image", 2), + ("decimal", 3), + ("date", 4), + ("datetime", 5), + ("gallery", 8), + ("video", 10), + ("objectives", 12), + ("carousel", 13), + ("callout", 14), + ("text_and_buttons", 20), + ("page", 21), + ("text_with_callable", 24), + ("block_with_name", 25), + ("advert", 26), + ("person", 27), + ("additional_interface_block", 28), + ], + block_lookup={ + 0: ( + "wagtail.blocks.CharBlock", + (), + {"form_classname": "full title"}, + ), + 1: ("wagtail.blocks.RichTextBlock", (), {}), + 2: ("wagtail.images.blocks.ImageChooserBlock", (), {}), + 3: ("wagtail.blocks.DecimalBlock", (), {}), + 4: ("wagtail.blocks.DateBlock", (), {}), + 5: ("wagtail.blocks.DateTimeBlock", (), {}), + 6: ( + "wagtail.blocks.StructBlock", + [[("caption", 0), ("image", 2)]], + {}, + ), + 7: ("wagtail.blocks.StreamBlock", [[("image", 6)]], {}), + 8: ( + "wagtail.blocks.StructBlock", + [[("title", 0), ("images", 7)]], + {}, + ), + 9: ("wagtail.embeds.blocks.EmbedBlock", (), {"required": False}), + 10: ("wagtail.blocks.StructBlock", [[("youtube_link", 9)]], {}), + 11: ("wagtail.blocks.CharBlock", (), {}), + 12: ("wagtail.blocks.ListBlock", (11,), {}), + 13: ( + "wagtail.blocks.StreamBlock", + [[("text", 0), ("image", 2), ("markup", 1)]], + {}, + ), + 14: ( + "wagtail.blocks.StructBlock", + [[("text", 1), ("image", 2)]], + {}, + ), + 15: ("wagtail.blocks.TextBlock", (), {}), + 16: ( + "wagtail.blocks.CharBlock", + (), + {"label": "Text", "max_length": 50, "required": True}, + ), + 17: ( + "wagtail.blocks.CharBlock", + (), + {"label": "Link", "max_length": 255, "required": True}, + ), + 18: ( + "wagtail.blocks.StructBlock", + [[("button_text", 16), ("button_link", 17)]], + {}, + ), + 19: ("wagtail.blocks.ListBlock", (18,), {}), + 20: ( + "wagtail.blocks.StructBlock", + [[("text", 15), ("buttons", 19), ("mainbutton", 18)]], + {}, + ), + 21: ("wagtail.blocks.PageChooserBlock", (), {}), + 22: ("wagtail.blocks.IntegerBlock", (), {}), + 23: ("wagtail.blocks.FloatBlock", (), {}), + 24: ( + "wagtail.blocks.StructBlock", + [ + [ + ("text", 11), + ("integer", 22), + ("decimal", 23), + ("page", 21), + ] + ], + {}, + ), + 25: ("wagtail.blocks.StructBlock", [[("name", 15)]], {}), + 26: ( + "wagtail.snippets.blocks.SnippetChooserBlock", + ("testapp.Advert",), + {}, + ), + 27: ( + "wagtail.snippets.blocks.SnippetChooserBlock", + ("testapp.Person",), + {}, + ), + 28: ("wagtail.blocks.StructBlock", [[("additional_text", 15)]], {}), + }, + ), + ), + ] diff --git a/tests/testapp/models/core.py b/tests/testapp/models/core.py index e010333f..732b1554 100644 --- a/tests/testapp/models/core.py +++ b/tests/testapp/models/core.py @@ -40,7 +40,7 @@ ) from grapple.utils import resolve_paginated_queryset from testapp.blocks import StreamFieldBlock -from testapp.interfaces import CustomInterface +from testapp.interfaces import AdditionalInterface document_model_string = getattr( @@ -50,7 +50,7 @@ @register_singular_query_field("simpleModel") class SimpleModel(models.Model): - graphql_interfaces = (CustomInterface,) + graphql_interfaces = (AdditionalInterface,) def custom_middleware_one(next, root, info, **args): @@ -83,7 +83,7 @@ class AuthorPage(Page): content_panels = Page.content_panels + [FieldPanel("name")] graphql_fields = [GraphQLString("name")] - graphql_interfaces = (CustomInterface,) + graphql_interfaces = (AdditionalInterface,) class BlogPageTag(TaggedItemBase): @@ -264,7 +264,7 @@ class Advert(models.Model): GraphQLString("string_rich_text", source="rich_text"), GraphQLString("extra_rich_text", deprecation_reason="Use rich_text instead"), ] - graphql_interfaces = (CustomInterface,) + graphql_interfaces = (AdditionalInterface,) def __str__(self): return self.text