@@ -25,8 +25,8 @@ This example model defines a Person, with a first and a last name:
25
25
last_name = graphene.String()
26
26
full_name = graphene.String()
27
27
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)
30
30
31
31
**first\_ name ** and **last\_ name ** are fields of the ObjectType. Each
32
32
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``\
56
56
so the first argument to the resolver method ``self `` (or ``root ``) need
57
57
not be an actual instance of the ``ObjectType ``.
58
58
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 ``.
59
62
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)
62
73
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:
66
80
67
81
.. code :: python
68
82
69
83
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()
70
92
71
93
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
73
200
74
- def resolve_reverse (self , info , word ):
75
- return word[::- 1 ]
76
201
77
202
Resolvers outside the class
78
203
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -83,11 +208,13 @@ A field can use a custom resolver from outside the class:
83
208
84
209
import graphene
85
210
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)
88
213
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)
91
218
92
219
93
220
Instances as data containers
0 commit comments