Skip to content

Commit e32cbc3

Browse files
authored
Merge pull request #883 from graphql-python/update-object-type-docs
Update documentation on ObjectTypes
2 parents 08c86f3 + d1b1ad7 commit e32cbc3

File tree

3 files changed

+143
-16
lines changed

3 files changed

+143
-16
lines changed

docs/quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ server with an associated set of resolve methods that know how to fetch
3030
data.
3131

3232
We are going to create a very simple schema, with a ``Query`` with only
33-
one field: ``hello`` and an input name. And when we query it, it should return ``"Hello
33+
one field: ``hello`` and an input name. And when we query it, it should return ``"Hello
3434
{argument}"``.
3535

3636
.. code:: python

docs/types/objecttypes.rst

Lines changed: 141 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ This example model defines a Person, with a first and a last name:
2525
last_name = graphene.String()
2626
full_name = graphene.String()
2727
28-
def resolve_full_name(self, info):
29-
return '{} {}'.format(self.first_name, self.last_name)
28+
def resolve_full_name(root, info):
29+
return '{} {}'.format(root.first_name, root.last_name)
3030
3131
**first\_name** and **last\_name** are fields of the ObjectType. Each
3232
field is specified as a class attribute, and each attribute maps to a
@@ -56,23 +56,148 @@ NOTE: The resolvers on an ``ObjectType`` are always treated as ``staticmethod``\
5656
so the first argument to the resolver method ``self`` (or ``root``) need
5757
not be an actual instance of the ``ObjectType``.
5858

59+
If an explicit resolver is not defined on the ``ObjectType`` then Graphene will
60+
attempt to use a property with the same name on the object that is passed to the
61+
``ObjectType``.
5962

60-
Quick example
61-
~~~~~~~~~~~~~
63+
.. code:: python
64+
65+
import graphene
66+
67+
class Person(graphene.ObjectType):
68+
first_name = graphene.String()
69+
last_name = graphene.String()
70+
71+
class Query(graphene.ObjectType):
72+
me = graphene.Field(Person)
6273
63-
This example model defines a ``Query`` type, which has a reverse field
64-
that reverses the given ``word`` argument using the ``resolve_reverse``
65-
method in the class.
74+
def resolve_me(_, info):
75+
# returns an object that represents a Person
76+
return get_human(name='Luke Skywalker')
77+
78+
If you are passing a dict instead of an object to your ``ObjectType`` you can
79+
change the default resolver in the ``Meta`` class like this:
6680

6781
.. code:: python
6882
6983
import graphene
84+
from graphene.types.resolver import dict_resolver
85+
86+
class Person(graphene.ObjectType):
87+
class Meta:
88+
default_resolver = dict_resolver
89+
90+
first_name = graphene.String()
91+
last_name = graphene.String()
7092
7193
class Query(graphene.ObjectType):
72-
reverse = graphene.String(word=graphene.String())
94+
me = graphene.Field(Person)
95+
96+
def resolve_me(_, info):
97+
return {
98+
"first_name": "Luke",
99+
"last_name": "Skywalker",
100+
}
101+
102+
Or you can change the default resolver globally by calling ``set_default_resolver``
103+
before executing a query.
104+
105+
.. code:: python
106+
107+
import graphene
108+
from graphene.types.resolver import dict_resolver, set_default_resolver
109+
110+
set_default_resolver(dict_resolver)
111+
112+
schema = graphene.Schema(query=Query)
113+
result = schema.execute('''
114+
query {
115+
me {
116+
firstName
117+
}
118+
}
119+
''')
120+
121+
122+
Resolvers with arguments
123+
~~~~~~~~~~~~~~~~~~~~~~~~
124+
125+
Any arguments that a field defines gets passed to the resolver function as
126+
kwargs. For example:
127+
128+
.. code:: python
129+
130+
import graphene
131+
132+
class Query(graphene.ObjectType):
133+
human_by_name = graphene.Field(Human, name=graphene.String(required=True))
134+
135+
def resolve_human_by_name(_, info, name):
136+
return get_human(name=name)
137+
138+
You can then execute the following query:
139+
140+
.. code::
141+
142+
query {
143+
humanByName(name: "Luke Skywalker") {
144+
firstName
145+
lastName
146+
}
147+
}
148+
149+
NOTE: if you define an argument for a field that is not required (and in a query
150+
execution it is not provided as an argument) it will not be passed to the
151+
resolver function at all. This is so that the developer can differenciate
152+
between a ``undefined`` value for an argument and an explicit ``null`` value.
153+
154+
For example, given this schema:
155+
156+
.. code:: python
157+
158+
import graphene
159+
160+
class Query(graphene.ObjectType):
161+
hello = graphene.String(required=True, name=graphene.String())
162+
163+
def resolve_hello(_, info, name):
164+
return name if name else 'World'
165+
166+
And this query:
167+
168+
.. code::
169+
170+
query {
171+
hello
172+
}
173+
174+
An error will be thrown:
175+
176+
.. code::
177+
178+
TypeError: resolve_hello() missing 1 required positional argument: 'name'
179+
180+
You can fix this error in 2 ways. Either by combining all keyword arguments
181+
into a dict:
182+
183+
.. code:: python
184+
185+
class Query(graphene.ObjectType):
186+
hello = graphene.String(required=True, name=graphene.String())
187+
188+
def resolve_hello(_, info, **args):
189+
return args.get('name', 'World')
190+
191+
Or by setting a default value for the keyword argument:
192+
193+
.. code:: python
194+
195+
class Query(graphene.ObjectType):
196+
hello = graphene.String(required=True, name=graphene.String())
197+
198+
def resolve_hello(_, info, name='World'):
199+
return name
73200
74-
def resolve_reverse(self, info, word):
75-
return word[::-1]
76201
77202
Resolvers outside the class
78203
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -83,11 +208,13 @@ A field can use a custom resolver from outside the class:
83208
84209
import graphene
85210
86-
def reverse(root, info, word):
87-
return word[::-1]
211+
def resolve_full_name(person, info):
212+
return '{} {}'.format(person.first_name, person.last_name)
88213
89-
class Query(graphene.ObjectType):
90-
reverse = graphene.String(word=graphene.String(), resolver=reverse)
214+
class Person(graphene.ObjectType):
215+
first_name = graphene.String()
216+
last_name = graphene.String()
217+
full_name = graphene.String(resolver=resolve_full_name)
91218
92219
93220
Instances as data containers

graphene/utils/str_converters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ def to_snake_case(name):
1818

1919

2020
def to_const(string):
21-
return re.sub("[\W|^]+", "_", string).upper()
21+
return re.sub("[\W|^]+", "_", string).upper() # noqa

0 commit comments

Comments
 (0)