|
1 | 1 | Interfaces
|
2 | 2 | ==========
|
3 | 3 |
|
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. |
6 | 6 |
|
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: |
17 | 9 |
|
18 | 10 | .. code:: python
|
19 | 11 |
|
20 | 12 | import graphene
|
21 | 13 |
|
22 | 14 | 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 |
24 | 27 |
|
25 |
| - # Human is a Character implementation |
26 | 28 | class Human(graphene.ObjectType):
|
27 | 29 | class Meta:
|
28 | 30 | interfaces = (Character, )
|
29 | 31 |
|
30 |
| - born_in = graphene.String() |
| 32 | + starships = graphene.List(Starship) |
| 33 | + home_planet = graphene.String() |
31 | 34 |
|
32 |
| - # Droid is a Character implementation |
33 | 35 | class Droid(graphene.ObjectType):
|
34 | 36 | class Meta:
|
35 | 37 | interfaces = (Character, )
|
36 | 38 |
|
37 |
| - function = graphene.String() |
| 39 | + primary_function = graphene.String() |
38 | 40 |
|
39 | 41 |
|
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. |
43 | 45 |
|
44 |
| -The above types have the following representation in a schema: |
| 46 | +The full GraphQL schema defition will look like this: |
45 | 47 |
|
46 | 48 | .. code::
|
47 | 49 |
|
48 | 50 | 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 |
50 | 64 | }
|
51 | 65 |
|
52 | 66 | 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 |
55 | 72 | }
|
56 | 73 |
|
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 | + } |
60 | 142 | }
|
0 commit comments