Grapple automatically builds GraphQL types by iterating over your project's models. To expose specific fields via GraphQL, add a graphql_fields attribute to your Wagtail model. This attribute should be a list of Grapple-specific field types (like GraphQLString, GraphQLRichText, or GraphQLStreamfield) that map to your model's existing fields.
When you define graphql_fields, the structure of the resulting GraphQL type is determined by the list you provide. For example, a StreamField in Wagtail can be exposed using GraphQLStreamfield to allow querying its block structure and raw values in GraphQL.
from django.db import models
from grapple.models import GraphQLRichText, GraphQLStreamfield, GraphQLString
from wagtail import blocks
from wagtail.models import Page
from wagtail.images.blocks import ImageBlock
from wagtail.fields import RichTextField, StreamField
from wagtail.admin.edit_handlers import FieldPanel
class BlogPage(Page):
author = models.CharField(max_length=255)
date = models.DateField("Post date")
summary = RichTextField()
body = StreamField(
[
("heading", blocks.CharBlock(classname="full title")),
("paragraph", blocks.RichTextBlock()),
("image", ImageBlock()),
]
)
content_panels = Page.content_panels + [
FieldPanel("author"),
FieldPanel("date"),
FieldPanel("summary"),
FieldPanel("body"),
]
# Define which fields are exposed to GraphQL
graphql_fields = [
GraphQLString("heading"),
GraphQLString("date"),
GraphQLString("author"),
GraphQLRichText("summary"),
GraphQLStreamfield("body"),
]