| 1 |
Building a Type Schema |
| 2 |
---------------------- |
| 3 |
|
| 4 |
.. currentmodule:: graphql.type |
| 5 |
|
| 6 |
Using the classes in the :mod:`graphql.type` sub-package as building blocks, you can |
| 7 |
build a complete GraphQL type schema. |
| 8 |
|
| 9 |
Let's take the following schema as an example, which will allow us to query our favorite |
| 10 |
heroes from the Star Wars trilogy:: |
| 11 |
|
| 12 |
enum Episode { NEWHOPE, EMPIRE, JEDI } |
| 13 |
|
| 14 |
interface Character { |
| 15 |
id: String! |
| 16 |
name: String |
| 17 |
friends: [Character] |
| 18 |
appearsIn: [Episode] |
| 19 |
} |
| 20 |
|
| 21 |
type Human implements Character { |
| 22 |
id: String! |
| 23 |
name: String |
| 24 |
friends: [Character] |
| 25 |
appearsIn: [Episode] |
| 26 |
homePlanet: String |
| 27 |
} |
| 28 |
|
| 29 |
type Droid implements Character { |
| 30 |
id: String! |
| 31 |
name: String |
| 32 |
friends: [Character] |
| 33 |
appearsIn: [Episode] |
| 34 |
primaryFunction: String |
| 35 |
} |
| 36 |
|
| 37 |
type Query { |
| 38 |
hero(episode: Episode): Character |
| 39 |
human(id: String!): Human |
| 40 |
droid(id: String!): Droid |
| 41 |
} |
| 42 |
|
| 43 |
We have been using the so called GraphQL schema definition language (SDL) here to |
| 44 |
describe the schema. While it is also possible to build a schema directly from this |
| 45 |
notation using GraphQL-core 3, let's first create that schema manually by assembling |
| 46 |
the types defined here using Python classes, adding resolver functions written in Python |
| 47 |
for querying the data. |
| 48 |
|
| 49 |
First, we need to import all the building blocks from the :mod:`graphql.type` |
| 50 |
sub-package. Note that you don't need to import from the sub-packages, since nearly |
| 51 |
everything is also available directly in the top :mod:`graphql` package:: |
| 52 |
|
| 53 |
from graphql import ( |
| 54 |
GraphQLArgument, GraphQLEnumType, GraphQLEnumValue, |
| 55 |
GraphQLField, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, |
| 56 |
GraphQLObjectType, GraphQLSchema, GraphQLString) |
| 57 |
|
| 58 |
Next, we need to build the enum type ``Episode``:: |
| 59 |
|
| 60 |
episode_enum = GraphQLEnumType('Episode', { |
| 61 |
'NEWHOPE': GraphQLEnumValue(4, description='Released in 1977.'), |
| 62 |
'EMPIRE': GraphQLEnumValue(5, description='Released in 1980.'), |
| 63 |
'JEDI': GraphQLEnumValue(6, description='Released in 1983.') |
| 64 |
}, description='One of the films in the Star Wars Trilogy') |
| 65 |
|
| 66 |
If you don't need the descriptions for the enum values, you can also define the enum |
| 67 |
type like this using an underlying Python ``Enum`` type:: |
| 68 |
|
| 69 |
from enum import Enum |
| 70 |
|
| 71 |
class EpisodeEnum(Enum): |
| 72 |
NEWHOPE = 4 |
| 73 |
EMPIRE = 5 |
| 74 |
JEDI = 6 |
| 75 |
|
| 76 |
episode_enum = GraphQLEnumType( |
| 77 |
'Episode', EpisodeEnum, |
| 78 |
description='One of the films in the Star Wars Trilogy') |
| 79 |
|
| 80 |
You can also use a Python dictionary instead of a Python ``Enum`` type to define the |
| 81 |
GraphQL enum type:: |
| 82 |
|
| 83 |
episode_enum = GraphQLEnumType( |
| 84 |
'Episode', {'NEWHOPE': 4, 'EMPIRE': 5, 'JEDI': 6}, |
| 85 |
description='One of the films in the Star Wars Trilogy') |
| 86 |
|
| 87 |
Our schema also contains a ``Character`` interface. Here is how we build it:: |
| 88 |
|
| 89 |
character_interface = GraphQLInterfaceType('Character', lambda: { |
| 90 |
'id': GraphQLField( |
| 91 |
GraphQLNonNull(GraphQLString), |
| 92 |
description='The id of the character.'), |
| 93 |
'name': GraphQLField( |
| 94 |
GraphQLString, |
| 95 |
description='The name of the character.'), |
| 96 |
'friends': GraphQLField( |
| 97 |
GraphQLList(character_interface), |
| 98 |
description='The friends of the character,' |
| 99 |
' or an empty list if they have none.'), |
| 100 |
'appearsIn': GraphQLField( |
| 101 |
GraphQLList(episode_enum), |
| 102 |
description='Which movies they appear in.'), |
| 103 |
'secretBackstory': GraphQLField( |
| 104 |
GraphQLString, |
| 105 |
description='All secrets about their past.')}, |
| 106 |
resolve_type=get_character_type, |
| 107 |
description='A character in the Star Wars Trilogy') |
| 108 |
|
| 109 |
Note that we did not pass the dictionary of fields to the ``GraphQLInterfaceType`` |
| 110 |
directly, but using a lambda function (a so-called "thunk"). This is necessary because |
| 111 |
the fields are referring back to the character interface that we are just defining. |
| 112 |
Whenever you have such recursive definitions in GraphQL-core, you need to use thunks. |
| 113 |
Otherwise, you can pass everything directly. |
| 114 |
|
| 115 |
Characters in the Star Wars trilogy are either humans or droids. So we define a |
| 116 |
``Human`` and a ``Droid`` type, which both implement the ``Character`` interface:: |
| 117 |
|
| 118 |
human_type = GraphQLObjectType('Human', lambda: { |
| 119 |
'id': GraphQLField( |
| 120 |
GraphQLNonNull(GraphQLString), |
| 121 |
description='The id of the human.'), |
| 122 |
'name': GraphQLField( |
| 123 |
GraphQLString, |
| 124 |
description='The name of the human.'), |
| 125 |
'friends': GraphQLField( |
| 126 |
GraphQLList(character_interface), |
| 127 |
description='The friends of the human,' |
| 128 |
' or an empty list if they have none.', |
| 129 |
resolve=get_friends), |
| 130 |
'appearsIn': GraphQLField( |
| 131 |
GraphQLList(episode_enum), |
| 132 |
description='Which movies they appear in.'), |
| 133 |
'homePlanet': GraphQLField( |
| 134 |
GraphQLString, |
| 135 |
description='The home planet of the human, or null if unknown.'), |
| 136 |
'secretBackstory': GraphQLField( |
| 137 |
GraphQLString, |
| 138 |
resolve=get_secret_backstory, |
| 139 |
description='Where are they from' |
| 140 |
' and how they came to be who they are.')}, |
| 141 |
interfaces=[character_interface], |
| 142 |
description='A humanoid creature in the Star Wars universe.') |
| 143 |
|
| 144 |
droid_type = GraphQLObjectType('Droid', lambda: { |
| 145 |
'id': GraphQLField( |
| 146 |
GraphQLNonNull(GraphQLString), |
| 147 |
description='The id of the droid.'), |
| 148 |
'name': GraphQLField( |
| 149 |
GraphQLString, |
| 150 |
description='The name of the droid.'), |
| 151 |
'friends': GraphQLField( |
| 152 |
GraphQLList(character_interface), |
| 153 |
description='The friends of the droid,' |
| 154 |
' or an empty list if they have none.', |
| 155 |
resolve=get_friends, |
| 156 |
), |
| 157 |
'appearsIn': GraphQLField( |
| 158 |
GraphQLList(episode_enum), |
| 159 |
description='Which movies they appear in.'), |
| 160 |
'secretBackstory': GraphQLField( |
| 161 |
GraphQLString, |
| 162 |
resolve=get_secret_backstory, |
| 163 |
description='Construction date and the name of the designer.'), |
| 164 |
'primaryFunction': GraphQLField( |
| 165 |
GraphQLString, |
| 166 |
description='The primary function of the droid.') |
| 167 |
}, |
| 168 |
interfaces=[character_interface], |
| 169 |
description='A mechanical creature in the Star Wars universe.') |
| 170 |
|
| 171 |
Now that we have defined all used result types, we can construct the ``Query`` type for |
| 172 |
our schema:: |
| 173 |
|
| 174 |
query_type = GraphQLObjectType('Query', lambda: { |
| 175 |
'hero': GraphQLField(character_interface, args={ |
| 176 |
'episode': GraphQLArgument(episode_enum, description=( |
| 177 |
'If omitted, returns the hero of the whole saga.' |
| 178 |
' If provided, returns the hero of that particular episode.'))}, |
| 179 |
resolve=get_hero), |
| 180 |
'human': GraphQLField(human_type, args={ |
| 181 |
'id': GraphQLArgument( |
| 182 |
GraphQLNonNull(GraphQLString), description='id of the human')}, |
| 183 |
resolve=get_human), |
| 184 |
'droid': GraphQLField(droid_type, args={ |
| 185 |
'id': GraphQLArgument( |
| 186 |
GraphQLNonNull(GraphQLString), description='id of the droid')}, |
| 187 |
resolve=get_droid)}) |
| 188 |
|
| 189 |
|
| 190 |
Using our query type we can define our schema:: |
| 191 |
|
| 192 |
schema = GraphQLSchema(query_type) |
| 193 |
|
| 194 |
Note that you can also pass a mutation type and a subscription type as additional |
| 195 |
arguments to the :class:`GraphQLSchema`. |