| 1 |
Implementing the Resolver Functions |
| 2 |
----------------------------------- |
| 3 |
|
| 4 |
.. currentmodule:: graphql.type |
| 5 |
|
| 6 |
Before we can execute queries against our schema, we also need to define the data (the |
| 7 |
humans and droids appearing in the Star Wars trilogy) and implement resolver functions |
| 8 |
that fetch the data (at the beginning of our schema module, because we are referencing |
| 9 |
them later):: |
| 10 |
|
| 11 |
luke = dict( |
| 12 |
id='1000', name='Luke Skywalker', homePlanet='Tatooine', |
| 13 |
friends=['1002', '1003', '2000', '2001'], appearsIn=[4, 5, 6]) |
| 14 |
|
| 15 |
vader = dict( |
| 16 |
id='1001', name='Darth Vader', homePlanet='Tatooine', |
| 17 |
friends=['1004'], appearsIn=[4, 5, 6]) |
| 18 |
|
| 19 |
han = dict( |
| 20 |
id='1002', name='Han Solo', homePlanet=None, |
| 21 |
friends=['1000', '1003', '2001'], appearsIn=[4, 5, 6]) |
| 22 |
|
| 23 |
leia = dict( |
| 24 |
id='1003', name='Leia Organa', homePlanet='Alderaan', |
| 25 |
friends=['1000', '1002', '2000', '2001'], appearsIn=[4, 5, 6]) |
| 26 |
|
| 27 |
tarkin = dict( |
| 28 |
id='1004', name='Wilhuff Tarkin', homePlanet=None, |
| 29 |
friends=['1001'], appearsIn=[4]) |
| 30 |
|
| 31 |
human_data = { |
| 32 |
'1000': luke, '1001': vader, '1002': han, '1003': leia, '1004': tarkin} |
| 33 |
|
| 34 |
threepio = dict( |
| 35 |
id='2000', name='C-3PO', primaryFunction='Protocol', |
| 36 |
friends=['1000', '1002', '1003', '2001'], appearsIn=[4, 5, 6]) |
| 37 |
|
| 38 |
artoo = dict( |
| 39 |
id='2001', name='R2-D2', primaryFunction='Astromech', |
| 40 |
friends=['1000', '1002', '1003'], appearsIn=[4, 5, 6]) |
| 41 |
|
| 42 |
droid_data = { |
| 43 |
'2000': threepio, '2001': artoo} |
| 44 |
|
| 45 |
|
| 46 |
def get_character_type(character, _info, _type): |
| 47 |
return 'Droid' if character['id'] in droid_data else 'Human' |
| 48 |
|
| 49 |
|
| 50 |
def get_character(id): |
| 51 |
"""Helper function to get a character by ID.""" |
| 52 |
return human_data.get(id) or droid_data.get(id) |
| 53 |
|
| 54 |
|
| 55 |
def get_friends(character, _info): |
| 56 |
"""Allows us to query for a character's friends.""" |
| 57 |
return map(get_character, character.friends) |
| 58 |
|
| 59 |
|
| 60 |
def get_hero(root, _info, episode): |
| 61 |
"""Allows us to fetch the undisputed hero of the trilogy, R2-D2.""" |
| 62 |
if episode == 5: |
| 63 |
return luke # Luke is the hero of Episode V |
| 64 |
return artoo # Artoo is the hero otherwise |
| 65 |
|
| 66 |
|
| 67 |
def get_human(root, _info, id): |
| 68 |
"""Allows us to query for the human with the given id.""" |
| 69 |
return human_data.get(id) |
| 70 |
|
| 71 |
|
| 72 |
def get_droid(root, _info, id): |
| 73 |
"""Allows us to query for the droid with the given id.""" |
| 74 |
return droid_data.get(id) |
| 75 |
|
| 76 |
|
| 77 |
def get_secret_backstory(_character, _info): |
| 78 |
"""Raise an error when attempting to get the secret backstory.""" |
| 79 |
raise RuntimeError('secretBackstory is secret.') |
| 80 |
|
| 81 |
|
| 82 |
Note that the resolver functions get the current object as first argument. For a field |
| 83 |
on the root Query type this is often not used, but a root object can also be defined |
| 84 |
when executing the query. As the second argument, they get an object containing |
| 85 |
execution information, as defined in the :class:`~GraphQLResolveInfo` class. |
| 86 |
This object also has a ``context`` attribute that can be used to provide every resolver |
| 87 |
with contextual information like the currently logged in user, or a database session. |
| 88 |
In our simple example we don't authenticate users and use static data instead of a |
| 89 |
database, so we don't make use of it here. In addition to these two arguments, |
| 90 |
resolver functions optionally get the defined for the field in the schema, using the |
| 91 |
same names (the names are not translated from GraphQL naming conventions to Python |
| 92 |
naming conventions). |
| 93 |
|
| 94 |
Also note that you don't need to provide resolvers for simple attribute access or for |
| 95 |
fetching items from Python dictionaries. |
| 96 |
|
| 97 |
Finally, note that our data uses the internal values of the ``Episode`` enum that we |
| 98 |
have defined above, not the descriptive enum names that are used externally. For |
| 99 |
example, ``NEWHOPE`` ("A New Hope") has internally the actual episode number 4 as value. |