2016-09-12 07:47:34 +03:00
|
|
|
Scalars
|
|
|
|
=======
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
Graphene defines the following base Scalar Types:
|
2016-09-21 11:16:35 +03:00
|
|
|
|
2016-09-12 07:47:34 +03:00
|
|
|
- ``graphene.String``
|
|
|
|
- ``graphene.Int``
|
|
|
|
- ``graphene.Float``
|
|
|
|
- ``graphene.Boolean``
|
|
|
|
- ``graphene.ID``
|
|
|
|
|
2016-11-23 02:04:22 +03:00
|
|
|
Graphene also provides custom scalars for Dates, Times, and JSON:
|
2016-09-21 11:16:35 +03:00
|
|
|
|
2017-11-29 16:32:17 +03:00
|
|
|
- ``graphene.types.datetime.Date``
|
2016-09-12 07:47:34 +03:00
|
|
|
- ``graphene.types.datetime.DateTime``
|
2016-11-23 02:04:22 +03:00
|
|
|
- ``graphene.types.datetime.Time``
|
2016-09-12 07:47:34 +03:00
|
|
|
- ``graphene.types.json.JSONString``
|
|
|
|
|
|
|
|
|
|
|
|
Custom scalars
|
|
|
|
--------------
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
You can create custom scalars for your schema.
|
2016-09-12 07:47:34 +03:00
|
|
|
The following is an example for creating a DateTime scalar:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
import datetime
|
2016-11-04 00:08:41 +03:00
|
|
|
from graphene.types import Scalar
|
|
|
|
from graphql.language import ast
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
class DateTime(Scalar):
|
|
|
|
'''DateTime Scalar Description'''
|
|
|
|
|
|
|
|
@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 Scalars
|
|
|
|
----------------
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
Scalars mounted in a ``ObjectType``, ``Interface`` or ``Mutation`` act as
|
|
|
|
``Field``\ s.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
class Person(graphene.ObjectType):
|
|
|
|
name = graphene.String()
|
|
|
|
|
|
|
|
# Is equivalent to:
|
|
|
|
class Person(graphene.ObjectType):
|
2016-10-16 21:30:51 +03:00
|
|
|
name = graphene.Field(graphene.String)
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
2016-11-04 00:17:37 +03:00
|
|
|
**Note:** when using the ``Field`` constructor directly, pass the type and
|
|
|
|
not an instance.
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
Types mounted in a ``Field`` act as ``Argument``\ s.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
2016-10-16 21:30:51 +03:00
|
|
|
graphene.Field(graphene.String, to=graphene.String())
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
# Is equivalent to:
|
2017-02-21 08:57:39 +03:00
|
|
|
graphene.Field(graphene.String, to=graphene.Argument(graphene.String))
|