3
3
Scalars
4
4
=======
5
5
6
+ Scalar types represent concrete values at the leaves of a query. There are
7
+ several built in types that Graphene provides out of the box which represent common
8
+ values in Python. You can also create your own Scalar types to better express
9
+ values that you might have in your data model.
10
+
6
11
All Scalar types accept the following arguments. All are optional:
7
12
8
13
``name ``: *string *
@@ -27,59 +32,223 @@ All Scalar types accept the following arguments. All are optional:
27
32
28
33
29
34
30
- Base scalars
31
- ------------
35
+ Built in scalars
36
+ ----------------
32
37
33
- Graphene defines the following base Scalar Types:
38
+ Graphene defines the following base Scalar Types that match the default ` GraphQL types < https://graphql.org/learn/schema/#scalar-types >`_ :
34
39
35
40
``graphene.String ``
41
+ ^^^^^^^^^^^^^^^^^^^
36
42
37
43
Represents textual data, represented as UTF-8
38
44
character sequences. The String type is most often used by GraphQL to
39
45
represent free-form human-readable text.
40
46
41
47
``graphene.Int ``
48
+ ^^^^^^^^^^^^^^^^
42
49
43
50
Represents non-fractional signed whole numeric
44
51
values. Int is a signed 32‐bit integer per the
45
52
`GraphQL spec <https://facebook.github.io/graphql/June2018/#sec-Int >`_
46
53
47
54
``graphene.Float ``
55
+ ^^^^^^^^^^^^^^^^^^
48
56
49
57
Represents signed double-precision fractional
50
58
values as specified by
51
59
`IEEE 754 <http://en.wikipedia.org/wiki/IEEE_floating_point >`_.
52
60
53
61
``graphene.Boolean ``
62
+ ^^^^^^^^^^^^^^^^^^^^
54
63
55
64
Represents `true ` or `false `.
56
65
57
66
``graphene.ID ``
67
+ ^^^^^^^^^^^^^^^
58
68
59
69
Represents a unique identifier, often used to
60
70
refetch an object or as key for a cache. The ID type appears in a JSON
61
71
response as a String; however, it is not intended to be human-readable.
62
72
When expected as an input type, any string (such as `"4" `) or integer
63
73
(such as `4 `) input value will be accepted as an ID.
64
74
65
- Graphene also provides custom scalars for Dates, Times, and JSON:
75
+ ----
66
76
67
- ``graphene.types.datetime.Date ``
77
+ Graphene also provides custom scalars for common values:
78
+
79
+ ``graphene.Date ``
80
+ ^^^^^^^^^^^^^^^^^
68
81
69
82
Represents a Date value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601 >`_.
70
83
71
- ``graphene.types.datetime.DateTime ``
84
+ .. code :: python
85
+
86
+ import datetime
87
+ from graphene import Schema, ObjectType, Date
88
+
89
+ class Query (ObjectType ):
90
+ one_week_from = Date(required = True , date_input = Date(required = True ))
91
+
92
+ def resolve_one_week_from (root , info , date_input ):
93
+ assert date_input == datetime.date(2006 , 1 , 2 )
94
+ return date_input + datetime.timedelta(weeks = 1 )
95
+
96
+ schema = Schema(query = Query)
97
+
98
+ results = schema.execute("""
99
+ query {
100
+ oneWeekFrom(dateInput: "2006-01-02")
101
+ }
102
+ """ )
103
+
104
+ assert results.data == {" oneWeekFrom" : " 2006-01-09" }
105
+
106
+
107
+ ``graphene.DateTime ``
108
+ ^^^^^^^^^^^^^^^^^^^^^
72
109
73
110
Represents a DateTime value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601 >`_.
74
111
75
- ``graphene.types.datetime.Time ``
112
+ .. code :: python
113
+
114
+ import datetime
115
+ from graphene import Schema, ObjectType, DateTime
116
+
117
+ class Query (ObjectType ):
118
+ one_hour_from = DateTime(required = True , datetime_input = DateTime(required = True ))
119
+
120
+ def resolve_one_hour_from (root , info , datetime_input ):
121
+ assert datetime_input == datetime.datetime(2006 , 1 , 2 , 15 , 4 , 5 )
122
+ return datetime_input + datetime.timedelta(hours = 1 )
123
+
124
+ schema = Schema(query = Query)
125
+
126
+ results = schema.execute("""
127
+ query {
128
+ oneHourFrom(datetimeInput: "2006-01-02T15:04:05")
129
+ }
130
+ """ )
131
+
132
+ assert results.data == {" oneHourFrom" : " 2006-01-02T16:04:05" }
133
+
134
+ ``graphene.Time ``
135
+ ^^^^^^^^^^^^^^^^^
76
136
77
137
Represents a Time value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601 >`_.
78
138
79
- ``graphene.types.json.JSONString ``
139
+ .. code :: python
140
+
141
+ import datetime
142
+ from graphene import Schema, ObjectType, Time
143
+
144
+ class Query (ObjectType ):
145
+ one_hour_from = Time(required = True , time_input = Time(required = True ))
146
+
147
+ def resolve_one_hour_from (root , info , time_input ):
148
+ assert time_input == datetime.time(15 , 4 , 5 )
149
+ tmp_time_input = datetime.datetime.combine(datetime.date(1 , 1 , 1 ), time_input)
150
+ return (tmp_time_input + datetime.timedelta(hours = 1 )).time()
151
+
152
+ schema = Schema(query = Query)
153
+
154
+ results = schema.execute("""
155
+ query {
156
+ oneHourFrom(timeInput: "15:04:05")
157
+ }
158
+ """ )
159
+
160
+ assert results.data == {" oneHourFrom" : " 16:04:05" }
161
+
162
+ ``graphene.Decimal ``
163
+ ^^^^^^^^^^^^^^^^^^^^
164
+
165
+ Represents a Python Decimal value.
166
+
167
+ .. code :: python
168
+
169
+ import decimal
170
+ from graphene import Schema, ObjectType, Decimal
171
+
172
+ class Query (ObjectType ):
173
+ add_one_to = Decimal(required = True , decimal_input = Decimal(required = True ))
174
+
175
+ def resolve_add_one_to (root , info , decimal_input ):
176
+ assert decimal_input == decimal.Decimal(" 10.50" )
177
+ return decimal_input + decimal.Decimal(" 1" )
178
+
179
+ schema = Schema(query = Query)
180
+
181
+ results = schema.execute("""
182
+ query {
183
+ addOneTo(decimalInput: "10.50")
184
+ }
185
+ """ )
186
+
187
+ assert results.data == {" addOneTo" : " 11.50" }
188
+
189
+ ``graphene.JSONString ``
190
+ ^^^^^^^^^^^^^^^^^^^^^^^
80
191
81
192
Represents a JSON string.
82
193
194
+ .. code :: python
195
+
196
+ from graphene import Schema, ObjectType, JSONString, String
197
+
198
+ class Query (ObjectType ):
199
+ update_json_key = JSONString(
200
+ required = True ,
201
+ json_input = JSONString(required = True ),
202
+ key = String(required = True ),
203
+ value = String(required = True )
204
+ )
205
+
206
+ def resolve_update_json_key (root , info , json_input , key , value ):
207
+ assert json_input == {" name" : " Jane" }
208
+ json_input[key] = value
209
+ return json_input
210
+
211
+ schema = Schema(query = Query)
212
+
213
+ results = schema.execute("""
214
+ query {
215
+ updateJsonKey(jsonInput: "{\\ "name\\ ": \\ "Jane\\ "}", key: "name", value: "Beth")
216
+ }
217
+ """ )
218
+
219
+ assert results.data == {" updateJsonKey" : " {\" name\" : \" Beth\" }" }
220
+
221
+
222
+ ``graphene.Base64 ``
223
+ ^^^^^^^^^^^^^^^^^^^
224
+
225
+ Represents a Base64 encoded string.
226
+
227
+ .. code :: python
228
+
229
+ from graphene import Schema, ObjectType, Base64
230
+
231
+ class Query (ObjectType ):
232
+ increment_encoded_id = Base64(
233
+ required = True ,
234
+ base64_input = Base64(required = True ),
235
+ )
236
+
237
+ def resolve_increment_encoded_id (root , info , base64_input ):
238
+ assert base64_input == " 4"
239
+ return int (base64_input) + 1
240
+
241
+ schema = Schema(query = Query)
242
+
243
+ results = schema.execute("""
244
+ query {
245
+ incrementEncodedId(base64Input: "NA==")
246
+ }
247
+ """ )
248
+
249
+ assert results.data == {" incrementEncodedId" : " NQ==" }
250
+
251
+
83
252
84
253
Custom scalars
85
254
--------------
0 commit comments