@@ -5,17 +5,59 @@ description: Walkthrough Basic Types
5
5
6
6
# Basic Types
7
7
8
- Graphene define the following base Types:
8
+ Graphene define the following base Scalar Types:
9
9
- ` graphene.String `
10
10
- ` graphene.Int `
11
11
- ` graphene.Float `
12
12
- ` graphene.Boolean `
13
13
- ` graphene.ID `
14
14
15
- Also, define :
15
+ Also the following Types are available :
16
16
- ` graphene.List `
17
17
- ` graphene.NonNull `
18
18
19
+ ## Shorcuts
20
+
21
+ There are some shorcuts for code easier.
22
+ The following are equivalent
23
+
24
+ ``` python
25
+ # A list of strings
26
+ string_list = graphene.List(graphene.String())
27
+ string_list = graphene.String().List
28
+
29
+ # A non-null string
30
+ string_non_null = graphene.String().NonNull
31
+ string_non_null = graphene.NonNull(graphene.String())
32
+ ```
33
+
34
+
35
+ ## Custom scalars
36
+
37
+ You can also create a custom scalar for your schema.
38
+ If you want to create a DateTime Scalar Type just type:
39
+
40
+ ``` python
41
+ import datetime
42
+ from graphql.core.language import ast
43
+
44
+ class DateTime (Scalar ):
45
+ ''' DateTime'''
46
+ @ staticmethod
47
+ def serialize (dt ):
48
+ return dt.isoformat()
49
+
50
+ @ staticmethod
51
+ def parse_literal (node ):
52
+ if isinstance (node, ast.StringValue):
53
+ return datetime.datetime.strptime(
54
+ node.value, " %Y-%m-%d T%H:%M:%S.%f " )
55
+
56
+ @ staticmethod
57
+ def parse_value (value ):
58
+ return datetime.datetime.strptime(value, " %Y-%m-%d T%H:%M:%S.%f " )
59
+ ```
60
+
19
61
## Mounting in ClassTypes
20
62
21
63
This types if are mounted in a ` ObjectType ` , ` Interface ` or ` Mutation ` ,
@@ -39,14 +81,10 @@ If this types are mounted in a `Field`, would act as `Argument`s.
39
81
So, the following examples will behave exactly the same:
40
82
41
83
``` python
42
- class Person (graphene .ObjectType ):
43
- say_hello = graphene.Field(graphene.String(),
44
- to = graphene.String())
84
+ graphene.Field(graphene.String(), to = graphene.String())
45
85
```
46
86
and
47
87
48
88
``` python
49
- class Person (graphene .ObjectType ):
50
- say_hello = graphene.Field(graphene.String(),
51
- to = graphene.Argument(graphene.String()))
89
+ graphene.Field(graphene.String(), to = graphene.Argument(graphene.String()))
52
90
```
0 commit comments