Skip to content

Commit ab25d07

Browse files
committed
explict imports from graphene
1 parent abe58cc commit ab25d07

File tree

3 files changed

+98
-69
lines changed

3 files changed

+98
-69
lines changed

docs/execution/execute.rst

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ For executing a query a schema, you can directly call the ``execute`` method on
99

1010
.. code:: python
1111
12-
schema = graphene.Schema(...)
12+
from graphene import Schema
13+
14+
schema = Schema(...)
1315
result = schema.execute('{ name }')
1416
1517
``result`` represents the result of execution. ``result.data`` is the result of executing the query, ``result.errors`` is ``None`` if no errors occurred, and is a non-empty list if an error occurred.
@@ -25,15 +27,17 @@ You can pass context to a query via ``context``.
2527

2628
.. code:: python
2729
28-
class Query(graphene.ObjectType):
29-
name = graphene.String()
30+
from graphene import ObjectType, String, Schema
31+
32+
class Query(ObjectType):
33+
name = String()
3034
3135
def resolve_name(root, info):
3236
return info.context.get('name')
3337
34-
schema = graphene.Schema(Query)
38+
schema = Schema(Query)
3539
result = schema.execute('{ name }', context={'name': 'Syrus'})
36-
40+
assert result.data['name'] == 'Syrus'
3741
3842
3943
Variables
@@ -44,13 +48,15 @@ You can pass variables to a query via ``variables``.
4448

4549
.. code:: python
4650
47-
class Query(graphene.ObjectType):
48-
user = graphene.Field(User, id=graphene.ID(required=True))
51+
from graphene import ObjectType, Field, ID, Schema
52+
53+
class Query(ObjectType):
54+
user = Field(User, id=ID(required=True))
4955
5056
def resolve_user(root, info, id):
5157
return get_user_by_id(id)
5258
53-
schema = graphene.Schema(Query)
59+
schema = Schema(Query)
5460
result = schema.execute(
5561
'''
5662
query getUser($id: ID) {
@@ -71,13 +77,15 @@ Value used for :ref:`ResolverRootArgument` in root queries and mutations can be
7177

7278
.. code:: python
7379
74-
class Query(graphene.ObjectType):
75-
me = graphene.Field(User)
80+
from graphene import ObjectType, Field, Schema
81+
82+
class Query(ObjectType):
83+
me = Field(User)
7684
7785
def resolve_user(root, info):
78-
return get_user_by_id(root.id)
86+
return {'id': root.id, 'firstName': root.name}
7987
80-
schema = graphene.Schema(Query)
88+
schema = Schema(Query)
8189
user_root = User(id=12, name='bob'}
8290
result = schema.execute(
8391
'''
@@ -91,6 +99,7 @@ Value used for :ref:`ResolverRootArgument` in root queries and mutations can be
9199
''',
92100
root=user_root
93101
)
102+
assert result.data['user']['id'] == user_root.id
94103
95104
Operation Name
96105
______________
@@ -99,13 +108,15 @@ If there are multiple operations defined in a query string, ``operation_name`` s
99108
100109
.. code:: python
101110
102-
class Query(graphene.ObjectType):
103-
me = graphene.Field(User)
111+
from graphene import ObjectType, Field, Schema
112+
113+
class Query(ObjectType):
114+
me = Field(User)
104115
105116
def resolve_user(root, info):
106117
return get_user_by_id(12)
107118
108-
schema = graphene.Schema(Query)
119+
schema = Schema(Query)
109120
query_string = '''
110121
query getUserWithFirstName {
111122
user {
@@ -117,12 +128,12 @@ If there are multiple operations defined in a query string, ``operation_name`` s
117128
query getUserWithFullName {
118129
user {
119130
id
120-
firstName
121-
lastName
131+
fullName
122132
}
123133
}
124134
'''
125135
result = schema.execute(
126136
query_string,
127137
operation_name='getUserWithFullName'
128138
)
139+
assert result.data['user']['fullName']

docs/quickstart.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ In Graphene, we can define a simple schema using the following code:
7676

7777
.. code:: python
7878
79-
import graphene
79+
from graphene import ObjectType, String, Schema
8080
81-
class Query(graphene.ObjectType):
81+
class Query(ObjectType):
8282
# this defines a Field `hello` in our Schema with a single Argument `name`
83-
hello = graphene.String(name=graphene.String(default_value="stranger"))
84-
goodbye = graphene.String()
83+
hello = String(name=String(default_value="stranger"))
84+
goodbye = String()
8585
8686
# our Resolver method takes the GraphQL context (root, info) as well as
8787
# Argument (name) for the Field and returns data for the query Response
@@ -91,7 +91,7 @@ In Graphene, we can define a simple schema using the following code:
9191
def resolve_goodbye(root, info):
9292
return 'See ya!'
9393
94-
schema = graphene.Schema(query=Query)
94+
schema = Schema(query=Query)
9595
9696
9797
A GraphQL **Schema** describes each **Field** in the data model provided by the server using scalar types like *String*, *Int* and *Enum* and compound types like *List* and *Object*. For more details refer to the Graphene :ref:`TypesReference`.

docs/types/objecttypes.rst

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ This example model defines a Person, with a first and a last name:
1818

1919
.. code:: python
2020
21-
class Person(graphene.ObjectType):
22-
first_name = graphene.String()
23-
last_name = graphene.String()
24-
full_name = graphene.String()
21+
from graphene import ObjectType, String
22+
23+
class Person(ObjectType):
24+
first_name = String()
25+
last_name = String()
26+
full_name = String()
2527
2628
def resolve_full_name(parent, info):
27-
return f'{parent.first_name} {parent.last_name}'
29+
return f"{parent.first_name} {parent.last_name}"
2830
2931
This *ObjectType* defines the feild **first\_name**, **last\_name**, and **full\_name**. Each field is specified as a class attribute, and each attribute maps to a Field. Data is fetched by our ``resolve_full_name`` :ref:`resolver method<Resolvers>` for ``full_name`` field and the :ref:`DefaultResolver` for other fields.
3032

@@ -56,8 +58,8 @@ Each resolver method takes the parameters:
5658

5759
.. _ResolverArguments:
5860

59-
Resolver Arguments
60-
~~~~~~~~~~~~~~~~~~
61+
Resolver Parameters
62+
~~~~~~~~~~~~~~~~~~~
6163

6264
.. _ResolverRootArgument:
6365

@@ -75,20 +77,20 @@ If we have a schema with Person type and one field on the root query.
7577

7678
.. code:: python
7779
78-
import graphene
80+
from graphene import ObjectType, String, Field
7981
80-
class Person(graphene.ObjectType):
81-
full_name = graphene.String()
82+
class Person(ObjectType):
83+
full_name = String()
8284
8385
def resolve_full_name(parent, info):
84-
return f'{parent.first_name} {parent.last_name}'
86+
return f"{parent.first_name} {parent.last_name}"
8587
86-
class Query(graphene.ObjectType):
87-
me = graphene.Field(Person)
88+
class Query(ObjectType):
89+
me = Field(Person)
8890
8991
def resolve_me(parent, info):
9092
# returns an object that represents a Person
91-
return get_human(name='Luke Skywalker')
93+
return get_human(name="Luke Skywalker")
9294
9395
When we execute a query against that schema.
9496

@@ -137,10 +139,10 @@ keyword arguments. For example:
137139
138140
.. code:: python
139141
140-
import graphene
142+
from graphene import ObjectType, Field, String
141143
142-
class Query(graphene.ObjectType):
143-
human_by_name = graphene.Field(Human, name=graphene.String(required=True))
144+
class Query(ObjectType):
145+
human_by_name = Field(Human, name=String(required=True))
144146
145147
def resolve_human_by_name(parent, info, name):
146148
return get_human(name=name)
@@ -170,9 +172,11 @@ The two resolvers in this example are effectively the same.
170172
171173
.. code:: python
172174
173-
class Person(graphene.ObjectType):
174-
first_name = graphene.String()
175-
last_name = graphene.String()
175+
from graphene import ObjectType, String
176+
177+
class Person(ObjectType):
178+
first_name = String()
179+
last_name = String()
176180
177181
@staticmethod
178182
def resolve_first_name(parent, info):
@@ -206,15 +210,17 @@ If the :ref:`ResolverRootArgument` is a dictionary, the resolver will look for a
206210
207211
from collections import namedtuple
208212
213+
from graphene import ObjectType, String, Field, Schema
214+
209215
PersonValueObject = namedtuple('Person', 'first_name', 'last_name')
210216
211-
class Person(graphene.ObjectType):
212-
first_name = graphene.String()
213-
last_name = graphene.String()
217+
class Person(ObjectType):
218+
first_name = String()
219+
last_name = String()
214220
215-
class Query(graphene.Object):
216-
me = graphene.Field(Person)
217-
my_best_friend = graphene.Field(Person)
221+
class Query(ObjectType):
222+
me = Field(Person)
223+
my_best_friend = Field(Person)
218224
219225
def resolve_me(parent, info):
220226
# always pass an object for `me` field
@@ -224,7 +230,7 @@ If the :ref:`ResolverRootArgument` is a dictionary, the resolver will look for a
224230
# always pass a dictionary for `my_best_fiend_field`
225231
return {"first_name": "R2", "last_name": "D2"}
226232
227-
schema = graphene.Schema(query=Query)
233+
schema = Schema(query=Query)
228234
result = schema.execute('''
229235
{
230236
me { firstName lastName }
@@ -252,10 +258,10 @@ For example, given this schema:
252258
253259
.. code:: python
254260
255-
import graphene
261+
from graphene import ObjectType, String
256262
257-
class Query(graphene.ObjectType):
258-
hello = graphene.String(required=True, name=graphene.String())
263+
class Query(ObjectType):
264+
hello = String(required=True, name=String())
259265
260266
def resolve_hello(parent, info, name):
261267
return name if name else 'World'
@@ -279,8 +285,10 @@ into a dict:
279285
280286
.. code:: python
281287
282-
class Query(graphene.ObjectType):
283-
hello = graphene.String(required=True, name=graphene.String())
288+
from graphene import ObjectType, String
289+
290+
class Query(ObjectType):
291+
hello = String(required=True, name=String())
284292
285293
def resolve_hello(parent, info, **kwargs):
286294
name = kwargs.get('name', 'World')
@@ -290,8 +298,10 @@ Or by setting a default value for the keyword argument:
290298
291299
.. code:: python
292300
293-
class Query(graphene.ObjectType):
294-
hello = graphene.String(required=True, name=graphene.String())
301+
from graphene import ObjectType, String
302+
303+
class Query(ObjectType):
304+
hello = String(required=True, name=String())
295305
296306
def resolve_hello(parent, info, name='World'):
297307
return f'Hello, {name}!'
@@ -300,10 +310,12 @@ One can also set a default value for an Argument in the GraphQL schema itself us
300310
301311
.. code:: python
302312
303-
class Query(graphene.ObjectType):
304-
hello = graphene.String(
313+
from graphene import ObjectType, String
314+
315+
class Query(ObjectType):
316+
hello = String(
305317
required=True,
306-
name=graphene.Argument(graphene.String, default_value='World')
318+
name=String(default_value='World')
307319
)
308320
309321
def resolve_hello(parent, info, name):
@@ -316,15 +328,15 @@ A field can use a custom resolver from outside the class:
316328
317329
.. code:: python
318330
319-
import graphene
331+
from graphene import ObjectType, String
320332
321333
def resolve_full_name(person, info):
322334
return '{} {}'.format(person.first_name, person.last_name)
323335
324-
class Person(graphene.ObjectType):
325-
first_name = graphene.String()
326-
last_name = graphene.String()
327-
full_name = graphene.String(resolver=resolve_full_name)
336+
class Person(ObjectType):
337+
first_name = String()
338+
last_name = String()
339+
full_name = String(resolver=resolve_full_name)
328340
329341
330342
Instances as value objects
@@ -337,8 +349,8 @@ previous example you could use ``Person`` to capture data for each of the *Objec
337349
338350
peter = Person(first_name='Peter', last_name='Griffin')
339351
340-
peter.first_name # prints "Peter"
341-
peter.last_name # prints "Griffin"
352+
peter.first_name # prints "Peter"
353+
peter.last_name # prints "Griffin"
342354
343355
Field camelcasing
344356
*****************
@@ -359,7 +371,9 @@ property on the ``Meta`` class:
359371
360372
.. code:: python
361373
362-
class MyGraphQlSong(graphene.ObjectType):
374+
from graphene import ObjectType
375+
376+
class MyGraphQlSong(ObjectType):
363377
class Meta:
364378
name = 'Song'
365379
@@ -370,7 +384,9 @@ The schema description of an *ObjectType* can be set as a docstring on the Pytho
370384
371385
.. code:: python
372386
373-
class MyGraphQlSong(graphene.ObjectType):
387+
from graphene import ObjectType
388+
389+
class MyGraphQlSong(ObjectType):
374390
''' We can set the schema description for an Object Type here on a docstring '''
375391
class Meta:
376392
description = 'But if we set the description in Meta, this value is used instead'
@@ -386,9 +402,11 @@ See :ref:`Interfaces` for more information.
386402
387403
.. code:: python
388404
405+
from graphene import ObjectType
406+
389407
Song = namedtuple('Song', ('title', 'artist'))
390408
391-
class MyGraphQlSong(graphene.ObjectType):
409+
class MyGraphQlSong(ObjectType):
392410
class Meta:
393411
interfaces = (graphene.Node, )
394412
possible_types = (Song, )

0 commit comments

Comments
 (0)