graphene/docs/types/interfaces.rst

61 lines
1.4 KiB
ReStructuredText
Raw Normal View History

Interfaces
==========
An Interface contains the essential fields that will be implemented by
multiple ObjectTypes.
The basics:
2016-09-21 11:16:35 +03:00
- Each Interface is a Python class that inherits from ``graphene.Interface``.
- Each attribute of the Interface represents a GraphQL field.
Quick example
-------------
This example model defines a ``Character`` interface with a name. ``Human``
and ``Droid`` are two implementations of that interface.
.. code:: python
import graphene
class Character(graphene.Interface):
name = graphene.String()
# Human is a Character implementation
2016-11-21 19:41:35 +03:00
class Human(graphene.ObjectType):
class Meta:
interfaces = (Character, )
born_in = graphene.String()
# Droid is a Character implementation
2016-11-21 19:41:35 +03:00
class Droid(graphene.ObjectType):
class Meta:
interfaces = (Character, )
function = graphene.String()
``name`` is a field on the ``Character`` interface that will also exist on both
the ``Human`` and ``Droid`` ObjectTypes (as those implement the ``Character``
interface). Each ObjectType may define additional fields.
The above types have the following representation in a schema:
2016-09-25 15:01:12 +03:00
.. code::
interface Character {
name: String
}
type Droid implements Character {
name: String
function: String
}
type Human implements Character {
name: String
bornIn: String
}