2016-09-12 07:47:34 +03:00
|
|
|
|
Scalars
|
|
|
|
|
=======
|
|
|
|
|
|
2017-12-10 17:53:28 +03:00
|
|
|
|
All Scalar types accept the following arguments. All are optional:
|
|
|
|
|
|
|
|
|
|
``name``: *string*
|
|
|
|
|
|
|
|
|
|
Override the name of the Field.
|
|
|
|
|
|
|
|
|
|
``description``: *string*
|
|
|
|
|
|
|
|
|
|
A description of the type to show in the GraphiQL browser.
|
|
|
|
|
|
|
|
|
|
``required``: *boolean*
|
|
|
|
|
|
|
|
|
|
If ``True``, the server will enforce a value for this field. See `NonNull <./list-and-nonnull.html#nonnull>`_. Default is ``False``.
|
|
|
|
|
|
|
|
|
|
``deprecation_reason``: *string*
|
|
|
|
|
|
|
|
|
|
Provide a deprecation reason for the Field.
|
|
|
|
|
|
|
|
|
|
``default_value``: *any*
|
|
|
|
|
|
|
|
|
|
Provide a default value for the Field.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Base scalars
|
|
|
|
|
------------
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
Graphene defines the following base Scalar Types:
|
2016-09-21 11:16:35 +03:00
|
|
|
|
|
2017-12-10 18:07:48 +03:00
|
|
|
|
``graphene.String``
|
|
|
|
|
|
|
|
|
|
Represents textual data, represented as UTF-8
|
|
|
|
|
character sequences. The String type is most often used by GraphQL to
|
|
|
|
|
represent free-form human-readable text.
|
|
|
|
|
|
|
|
|
|
``graphene.Int``
|
|
|
|
|
|
|
|
|
|
Represents non-fractional signed whole numeric
|
2019-03-11 19:32:23 +03:00
|
|
|
|
values. Int is a signed 32‐bit integer per the
|
|
|
|
|
`GraphQL spec <https://facebook.github.io/graphql/June2018/#sec-Int>`_
|
2017-12-10 18:07:48 +03:00
|
|
|
|
|
|
|
|
|
``graphene.Float``
|
|
|
|
|
|
|
|
|
|
Represents signed double-precision fractional
|
|
|
|
|
values as specified by
|
|
|
|
|
`IEEE 754 <http://en.wikipedia.org/wiki/IEEE_floating_point>`_.
|
|
|
|
|
|
|
|
|
|
``graphene.Boolean``
|
|
|
|
|
|
|
|
|
|
Represents `true` or `false`.
|
|
|
|
|
|
|
|
|
|
``graphene.ID``
|
|
|
|
|
|
|
|
|
|
Represents a unique identifier, often used to
|
|
|
|
|
refetch an object or as key for a cache. The ID type appears in a JSON
|
|
|
|
|
response as a String; however, it is not intended to be human-readable.
|
|
|
|
|
When expected as an input type, any string (such as `"4"`) or integer
|
|
|
|
|
(such as `4`) input value will be accepted as an ID.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
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-12-10 18:07:48 +03:00
|
|
|
|
``graphene.types.datetime.Date``
|
|
|
|
|
|
|
|
|
|
Represents a Date value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_.
|
|
|
|
|
|
|
|
|
|
``graphene.types.datetime.DateTime``
|
|
|
|
|
|
|
|
|
|
Represents a DateTime value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_.
|
|
|
|
|
|
|
|
|
|
``graphene.types.datetime.Time``
|
|
|
|
|
|
|
|
|
|
Represents a Time value as specified by `iso8601 <https://en.wikipedia.org/wiki/ISO_8601>`_.
|
|
|
|
|
|
|
|
|
|
``graphene.types.json.JSONString``
|
|
|
|
|
|
|
|
|
|
Represents a JSON string.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|