Interfaces¶
Out of the box, Grapple generates the schema with the following GraphQL interfaces:
PageInterfacefor WagtailPagemodelsStreamFieldInterfacefor WagtailStreamFieldblock models
PageInterface¶
The default GraphQL interface for all Wagtail Page-derived models.
This is accessible through the pages or page field on the root query type.
The interface exposes the following fields, following the Wagtail Page model fields and properties:
id: ID
url: String
slug: String
depth: Int
pageType: String
title: String
seoTitle: String
seoDescription: String
showInMenus: Boolean
contentType: String
parent: PageInterface
children(limit: PositiveInt, offset: PositiveInt, order: String, searchQuery: String, searchOperator: SearchOperatorEnum, id: ID): [PageInterface]
siblings(limit: PositiveInt, offset: PositiveInt, order: String, searchQuery: String, searchOperator: SearchOperatorEnum, id: ID): [PageInterface]
nextSiblings(limit: PositiveInt, offset: PositiveInt, order: String, searchQuery: String, searchOperator: SearchOperatorEnum, id: ID): [PageInterface]
previousSiblings(limit: PositiveInt, offset: PositiveInt, order: String, searchQuery: String, searchOperator: SearchOperatorEnum, id: ID): [PageInterface]
descendants(limit: PositiveInt, offset: PositiveInt, order: String, searchQuery: String, searchOperator: SearchOperatorEnum, id: ID): [PageInterface]
ancestors(limit: PositiveInt, offset: PositiveInt, order: String, searchQuery: String, searchOperator: SearchOperatorEnum, id: ID): [PageInterface]
Any custom graphql_fields added to your specific Page models will be available here via the ‘on’ spread operator and
the name of the model:
query {
pages {
...on BlogPage {
the_custom_field
}
}
}
You can change the default PageInterface to your own interface by changing the
PAGE_INTERFACE setting.
As mentioned above there is both a plural pages and singular page
field on the root Query type that returns a PageInterface.
The plural pages field (as do all plural fields)
accepts the following arguments:
id: ID
limit: PositiveInt
offset: PositiveInt
order: String
searchQuery: String
searchOperator: OR | AND # default is AND
contentType: String # comma separated list of content types in app.Model notation
inSite: Boolean
ancestor: PositiveInt # ID of ancestor page to restrict results to
parent: PositiveInt # ID of parent page to restrict results to
The singular page field accepts the following arguments:
id: ID # Can be used on it's own
slug: String # Can be used on it's own
urlPath: String # Can be used on it's own
token: String # Must be used with one of the others. Usually contentType
contentType: String # Can be used on it's own
inSite: Boolean # Can be used on it's own
StreamFieldInterface¶
StreamFieldInterface is the default interface for all Wagtail StreamField block models. It exposes the following
fields, following the base fields and properties available to the Wagtail StreamField blocks:
id: ID
blockType: String!
field: String!
rawValue: String!
Note that blocks subclassing StreamBlock and StructBlock have an additional property in the interface:
blocks: [StreamFieldInterface!]! # a list of blocks in the StreamBlock or StructBlock
SnippetInterface¶
SnippetInterface is the default interface for all Wagtail snippet models. It is accessible through the
snippets field on the root query type. It exposes the following fields:
snippetType: String!
contentType: String!
An example of querying all snippets:
query {
snippets {
snippetType
contentType
...on Advert {
id
url
text
}
}
}
You can change the default SnippetInterface to your own interface by changing the
SNIPPET_INTERFACE setting.
Adding your own interfaces¶
To add additional interfaces to your model, define the graphql_interfaces attribute on it. The attribute can be
a list of interfaces (graphql_interfaces = [MyInterface]) or a tuple (graphql_interfaces = (MyInterface, )).
Given the following example interface:
# interfaces.py
class MyInterface(graphene.Interface):
custom_field = graphene.String()
you could add it to your Page model like so:
from wagtail.models import Page
from .interfaces import MyInterface
class MyPage(Page):
# ...
graphql_interfaces = (MyInterface,)
or any Django model:
# models.py
from django.db import models
from .interfaces import MyInterface
class MyModel(models.Model):
# ...
graphql_interfaces = (MyInterface,)
or a StreamField block:
# blocks.py
from wagtail.core import blocks
from .interfaces import MyInterface
class MyStructBlock(blocks.StructBlock):
# ...
graphql_interfaces = (MyInterface,)
The provided interfaces will be added to the base interfaces for the model.