You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/execution/execute.rst
+28-17Lines changed: 28 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,9 @@ For executing a query a schema, you can directly call the ``execute`` method on
9
9
10
10
.. code:: python
11
11
12
-
schema = graphene.Schema(...)
12
+
from graphene import Schema
13
+
14
+
schema = Schema(...)
13
15
result = schema.execute('{ name }')
14
16
15
17
``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``.
25
27
26
28
.. code:: python
27
29
28
-
classQuery(graphene.ObjectType):
29
-
name = graphene.String()
30
+
from graphene import ObjectType, String, Schema
31
+
32
+
classQuery(ObjectType):
33
+
name = String()
30
34
31
35
defresolve_name(root, info):
32
36
return info.context.get('name')
33
37
34
-
schema =graphene.Schema(Query)
38
+
schema = Schema(Query)
35
39
result = schema.execute('{ name }', context={'name': 'Syrus'})
36
-
40
+
assert result.data['name'] =='Syrus'
37
41
38
42
39
43
Variables
@@ -44,13 +48,15 @@ You can pass variables to a query via ``variables``.
44
48
45
49
.. code:: python
46
50
47
-
classQuery(graphene.ObjectType):
48
-
user = graphene.Field(User, id=graphene.ID(required=True))
51
+
from graphene import ObjectType, Field, ID, Schema
52
+
53
+
classQuery(ObjectType):
54
+
user = Field(User, id=ID(required=True))
49
55
50
56
defresolve_user(root, info, id):
51
57
return get_user_by_id(id)
52
58
53
-
schema =graphene.Schema(Query)
59
+
schema = Schema(Query)
54
60
result = schema.execute(
55
61
'''
56
62
query getUser($id: ID) {
@@ -71,13 +77,15 @@ Value used for :ref:`ResolverRootArgument` in root queries and mutations can be
71
77
72
78
.. code:: python
73
79
74
-
classQuery(graphene.ObjectType):
75
-
me = graphene.Field(User)
80
+
from graphene import ObjectType, Field, Schema
81
+
82
+
classQuery(ObjectType):
83
+
me = Field(User)
76
84
77
85
defresolve_user(root, info):
78
-
returnget_user_by_id(root.id)
86
+
return{'id': root.id, 'firstName': root.name}
79
87
80
-
schema =graphene.Schema(Query)
88
+
schema = Schema(Query)
81
89
user_root = User(id=12, name='bob'}
82
90
result= schema.execute(
83
91
'''
@@ -91,6 +99,7 @@ Value used for :ref:`ResolverRootArgument` in root queries and mutations can be
91
99
''',
92
100
root=user_root
93
101
)
102
+
assert result.data['user']['id'] == user_root.id
94
103
95
104
Operation Name
96
105
______________
@@ -99,13 +108,15 @@ If there are multiple operations defined in a query string, ``operation_name`` s
99
108
100
109
.. code:: python
101
110
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)
104
115
105
116
def resolve_user(root, info):
106
117
return get_user_by_id(12)
107
118
108
-
schema=graphene.Schema(Query)
119
+
schema= Schema(Query)
109
120
query_string='''
110
121
query getUserWithFirstName {
111
122
user {
@@ -117,12 +128,12 @@ If there are multiple operations defined in a query string, ``operation_name`` s
# our Resolver method takes the GraphQL context (root, info) as well as
87
87
# 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:
91
91
defresolve_goodbye(root, info):
92
92
return'See ya!'
93
93
94
-
schema =graphene.Schema(query=Query)
94
+
schema = Schema(query=Query)
95
95
96
96
97
97
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`.
Copy file name to clipboardExpand all lines: docs/types/objecttypes.rst
+65-47Lines changed: 65 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -18,13 +18,15 @@ This example model defines a Person, with a first and a last name:
18
18
19
19
.. code:: python
20
20
21
-
classPerson(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
+
classPerson(ObjectType):
24
+
first_name = String()
25
+
last_name = String()
26
+
full_name = String()
25
27
26
28
defresolve_full_name(parent, info):
27
-
returnf'{parent.first_name}{parent.last_name}'
29
+
returnf"{parent.first_name}{parent.last_name}"
28
30
29
31
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.
30
32
@@ -56,8 +58,8 @@ Each resolver method takes the parameters:
56
58
57
59
.. _ResolverArguments:
58
60
59
-
Resolver Arguments
60
-
~~~~~~~~~~~~~~~~~~
61
+
Resolver Parameters
62
+
~~~~~~~~~~~~~~~~~~~
61
63
62
64
.. _ResolverRootArgument:
63
65
@@ -75,20 +77,20 @@ If we have a schema with Person type and one field on the root query.
75
77
76
78
.. code:: python
77
79
78
-
import graphene
80
+
from grapheneimport ObjectType, String, Field
79
81
80
-
classPerson(graphene.ObjectType):
81
-
full_name =graphene.String()
82
+
classPerson(ObjectType):
83
+
full_name = String()
82
84
83
85
defresolve_full_name(parent, info):
84
-
returnf'{parent.first_name}{parent.last_name}'
86
+
returnf"{parent.first_name}{parent.last_name}"
85
87
86
-
classQuery(graphene.ObjectType):
87
-
me =graphene.Field(Person)
88
+
classQuery(ObjectType):
89
+
me = Field(Person)
88
90
89
91
defresolve_me(parent, info):
90
92
# returns an object that represents a Person
91
-
return get_human(name='Luke Skywalker')
93
+
return get_human(name="Luke Skywalker")
92
94
93
95
When we execute a query against that schema.
94
96
@@ -137,10 +139,10 @@ keyword arguments. For example:
0 commit comments