Skip to content

Commit 43e8776

Browse files
committed
Update interface documentation
1 parent c102458 commit 43e8776

File tree

1 file changed

+109
-27
lines changed

1 file changed

+109
-27
lines changed

docs/types/interfaces.rst

Lines changed: 109 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,142 @@
11
Interfaces
22
==========
33

4-
An Interface contains the essential fields that will be implemented by
5-
multiple ObjectTypes.
4+
An *Interface* is an abstract type that defines a certain set of fields that a
5+
type must include to implement the interface.
66

7-
The basics:
8-
9-
- Each Interface is a Python class that inherits from ``graphene.Interface``.
10-
- Each attribute of the Interface represents a GraphQL field.
11-
12-
Quick example
13-
-------------
14-
15-
This example model defines a ``Character`` interface with a name. ``Human``
16-
and ``Droid`` are two implementations of that interface.
7+
For example, you can define an Interface ``Character`` that represents any
8+
character in the Star Wars trilogy:
179

1810
.. code:: python
1911
2012
import graphene
2113
2214
class Character(graphene.Interface):
23-
name = graphene.String()
15+
id = graphene.ID(required=True)
16+
name = graphene.String(required=True)
17+
friends = graphene.List(lambda: Character)
18+
appears_in = graphene.List(Episode, required=True)
19+
20+
21+
Any ObjectType that implements ``Character`` will have these exact fields, with
22+
these arguments and return types.
23+
24+
For example, here are some types that might implement ``Character``:
25+
26+
.. code:: python
2427
25-
# Human is a Character implementation
2628
class Human(graphene.ObjectType):
2729
class Meta:
2830
interfaces = (Character, )
2931
30-
born_in = graphene.String()
32+
starships = graphene.List(Starship)
33+
home_planet = graphene.String()
3134
32-
# Droid is a Character implementation
3335
class Droid(graphene.ObjectType):
3436
class Meta:
3537
interfaces = (Character, )
3638
37-
function = graphene.String()
39+
primary_function = graphene.String()
3840
3941
40-
``name`` is a field on the ``Character`` interface that will also exist on both
41-
the ``Human`` and ``Droid`` ObjectTypes (as those implement the ``Character``
42-
interface). Each ObjectType may define additional fields.
42+
Both of these types have all of the fields from the ``Character`` interface,
43+
but also bring in extra fields, ``home_planet``, ``starships`` and
44+
``primary_function``, that are specific to that particular type of character.
4345

44-
The above types have the following representation in a schema:
46+
The full GraphQL schema defition will look like this:
4547

4648
.. code::
4749
4850
interface Character {
49-
name: String
51+
id: ID!
52+
name: String!
53+
friends: [Character]
54+
appearsIn: [Episode]!
55+
}
56+
57+
type Human implements Character {
58+
id: ID!
59+
name: String!
60+
friends: [Character]
61+
appearsIn: [Episode]!
62+
starships: [Starship]
63+
homePlanet: String
5064
}
5165
5266
type Droid implements Character {
53-
name: String
54-
function: String
67+
id: ID!
68+
name: String!
69+
friends: [Character]
70+
appearsIn: [Episode]!
71+
primaryFunction: String
5572
}
5673
57-
type Human implements Character {
58-
name: String
59-
bornIn: String
74+
Interfaces are useful when you want to return an object or set of objects,
75+
which might be of several different types.
76+
77+
For example, you can define a field ``hero`` that resolves to any
78+
``Character``, depending on the episode, like this:
79+
80+
.. code:: python
81+
82+
class Query(graphene.ObjectType):
83+
hero = graphene.Field(
84+
Character,
85+
required=True,
86+
episode=graphene.Field(Episode, required=True)
87+
)
88+
89+
def resolve_hero(_, info, episode):
90+
# Luke is the hero of Episode V
91+
if episode == 5:
92+
return get_human(name='Luke Skywalker')
93+
return get_droid(name='R2-D2')
94+
95+
This allows you to directly query for fields that exist on the Character interface
96+
as well as selecting specific fields on any type that implments the interface
97+
using `inline fragments <https://graphql.org/learn/queries/#inline-fragments>`_.
98+
99+
For example, the following query:
100+
101+
.. code::
102+
103+
query HeroForEpisode($episode: Episode!) {
104+
hero(episode: $episode) {
105+
__typename
106+
name
107+
... on Droid {
108+
primaryFunction
109+
}
110+
... on Human {
111+
homePlanet
112+
}
113+
}
114+
}
115+
116+
Will return the following data with variables ``{ "episode": 4 }``:
117+
118+
.. code:: json
119+
120+
{
121+
"data": {
122+
"hero": {
123+
"__typename": "Droid",
124+
"name": "R2-D2",
125+
"primaryFunction": "Astromech"
126+
}
127+
}
128+
}
129+
130+
And different data with the variables ``{ "episode": 5 }``:
131+
132+
.. code:: json
133+
134+
{
135+
"data": {
136+
"hero": {
137+
"__typename": "Human",
138+
"name": "Luke Skywalker",
139+
"homePlanet": "Tatooine"
140+
}
141+
}
60142
}

0 commit comments

Comments
 (0)