mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 11:33:55 +03:00
Improved basic types page
This commit is contained in:
parent
324aa9bd84
commit
aebafbc3c3
|
@ -5,17 +5,59 @@ description: Walkthrough Basic Types
|
|||
|
||||
# Basic Types
|
||||
|
||||
Graphene define the following base Types:
|
||||
Graphene define the following base Scalar Types:
|
||||
- `graphene.String`
|
||||
- `graphene.Int`
|
||||
- `graphene.Float`
|
||||
- `graphene.Boolean`
|
||||
- `graphene.ID`
|
||||
|
||||
Also, define:
|
||||
Also the following Types are available:
|
||||
- `graphene.List`
|
||||
- `graphene.NonNull`
|
||||
|
||||
## Shorcuts
|
||||
|
||||
There are some shorcuts for code easier.
|
||||
The following are equivalent
|
||||
|
||||
```python
|
||||
# A list of strings
|
||||
string_list = graphene.List(graphene.String())
|
||||
string_list = graphene.String().List
|
||||
|
||||
# A non-null string
|
||||
string_non_null = graphene.String().NonNull
|
||||
string_non_null = graphene.NonNull(graphene.String())
|
||||
```
|
||||
|
||||
|
||||
## Custom scalars
|
||||
|
||||
You can also create a custom scalar for your schema.
|
||||
If you want to create a DateTime Scalar Type just type:
|
||||
|
||||
```python
|
||||
import datetime
|
||||
from graphql.core.language import ast
|
||||
|
||||
class DateTime(Scalar):
|
||||
'''DateTime'''
|
||||
@staticmethod
|
||||
def serialize(dt):
|
||||
return dt.isoformat()
|
||||
|
||||
@staticmethod
|
||||
def parse_literal(node):
|
||||
if isinstance(node, ast.StringValue):
|
||||
return datetime.datetime.strptime(
|
||||
node.value, "%Y-%m-%dT%H:%M:%S.%f")
|
||||
|
||||
@staticmethod
|
||||
def parse_value(value):
|
||||
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
|
||||
```
|
||||
|
||||
## Mounting in ClassTypes
|
||||
|
||||
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.
|
|||
So, the following examples will behave exactly the same:
|
||||
|
||||
```python
|
||||
class Person(graphene.ObjectType):
|
||||
say_hello = graphene.Field(graphene.String(),
|
||||
to=graphene.String())
|
||||
graphene.Field(graphene.String(), to=graphene.String())
|
||||
```
|
||||
and
|
||||
|
||||
```python
|
||||
class Person(graphene.ObjectType):
|
||||
say_hello = graphene.Field(graphene.String(),
|
||||
to=graphene.Argument(graphene.String()))
|
||||
graphene.Field(graphene.String(), to=graphene.Argument(graphene.String()))
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue
Block a user