From aebafbc3c3f58518a767f0a15eb8f25f7c0c0e8b Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 25 Nov 2015 22:02:36 -0800 Subject: [PATCH] Improved basic types page --- docs/pages/docs/basic-types.md | 54 +++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/docs/pages/docs/basic-types.md b/docs/pages/docs/basic-types.md index 7a067942..50e96e86 100644 --- a/docs/pages/docs/basic-types.md +++ b/docs/pages/docs/basic-types.md @@ -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())) ```