Skip to content

Commit

Permalink
Rename CustomInterface to AdditionalInterface
Browse files Browse the repository at this point in the history
The name `CustomInterface` is too similar with `CustomPageInterface` and
the upcoming `CustomSnippetInterface` and their custom interfaces
settings file. Better rename it to `AdditionalInterface` so there is
less confusion.
  • Loading branch information
mgax committed Sep 20, 2024
1 parent db86c97 commit d092770
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 41 deletions.
6 changes: 3 additions & 3 deletions docs/general-usage/decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,)
15 changes: 7 additions & 8 deletions docs/general-usage/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,49 +118,48 @@ 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:

.. 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:

.. code-block:: python
# 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:

.. code-block:: python
# 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.
26 changes: 13 additions & 13 deletions tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +21,7 @@ def setUpTestData(cls):

cls.blog_page = BlogPageFactory(
body=[
("custom_interface_block", CustomInterfaceBlockFactory()),
("additional_interface_block", AdditionalInterfaceBlockFactory()),
],
parent=cls.home,
)
Expand All @@ -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
}
}
}
Expand All @@ -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"}],
)


Expand Down
14 changes: 7 additions & 7 deletions tests/testapp/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
GraphQLStreamfield,
GraphQLString,
)
from testapp.interfaces import CustomInterface
from testapp.interfaces import AdditionalInterface


if TYPE_CHECKING:
Expand Down Expand Up @@ -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):
Expand All @@ -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()
8 changes: 4 additions & 4 deletions tests/testapp/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from wagtail.contrib.redirects.models import Redirect

from testapp.blocks import (
CustomInterfaceBlock,
AdditionalInterfaceBlock,
ImageGalleryBlock,
ImageGalleryImage,
ImageGalleryImages,
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/testapp/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
126 changes: 126 additions & 0 deletions tests/testapp/migrations/0003_alter_blogpage_body.py
Original file line number Diff line number Diff line change
@@ -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)]], {}),
},
),
),
]
8 changes: 4 additions & 4 deletions tests/testapp/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d092770

Please sign in to comment.