Skip to content

Commit aebafbc

Browse files
committed
Improved basic types page
1 parent 324aa9b commit aebafbc

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

docs/pages/docs/basic-types.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,59 @@ description: Walkthrough Basic Types
55

66
# Basic Types
77

8-
Graphene define the following base Types:
8+
Graphene define the following base Scalar Types:
99
- `graphene.String`
1010
- `graphene.Int`
1111
- `graphene.Float`
1212
- `graphene.Boolean`
1313
- `graphene.ID`
1414

15-
Also, define:
15+
Also the following Types are available:
1616
- `graphene.List`
1717
- `graphene.NonNull`
1818

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-%dT%H:%M:%S.%f")
55+
56+
@staticmethod
57+
def parse_value(value):
58+
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
59+
```
60+
1961
## Mounting in ClassTypes
2062

2163
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.
3981
So, the following examples will behave exactly the same:
4082

4183
```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())
4585
```
4686
and
4787

4888
```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()))
5290
```

0 commit comments

Comments
 (0)