All Scalar types accept the following arguments. All are optional:
Override the name of the Field.
A description of the type to show in the GraphiQL browser.
True, the server will enforce a value for this field. See NonNull. Default is
Provide a deprecation reason for the Field.
Provide a default value for the Field.
Graphene defines the following base Scalar Types:
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.
Represents non-fractional signed whole numeric values. Int is a signed 32‐bit integer per the GraphQL spec
Represents signed double-precision fractional values as specified by IEEE 754.
Represents true or false.
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.
Graphene also provides custom scalars for Dates, Times, and JSON:
Represents a Date value as specified by iso8601.
Represents a DateTime value as specified by iso8601.
Represents a Time value as specified by iso8601.
Represents a JSON string.
You can create custom scalars for your schema. The following is an example for creating a DateTime scalar:
import datetime from graphene.types import Scalar from graphql.language import ast 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")
Scalars mounted in a
Mutation act as
class Person(graphene.ObjectType): name = graphene.String() # Is equivalent to: class Person(graphene.ObjectType): name = graphene.Field(graphene.String)
Note: when using the
Field constructor directly, pass the type and
not an instance.
Types mounted in a
Field act as
graphene.Field(graphene.String, to=graphene.String()) # Is equivalent to: graphene.Field(graphene.String, to=graphene.Argument(graphene.String))