Supports for Json literals
authorLaurent Wouters <lwouters@cenotelie.fr>
Thu, 13 Dec 2018 15:01:22 +0100
changeset 6 1d29811213b2
parent 5 75a26ebb659f
child 7 caf60851619d
Supports for Json literals
cubicweb_graphql/gql_schema.py
--- a/cubicweb_graphql/gql_schema.py	Thu Jun 21 18:25:13 2018 +0200
+++ b/cubicweb_graphql/gql_schema.py	Thu Dec 13 15:01:22 2018 +0100
@@ -22,6 +22,7 @@
 import graphene.types.datetime
 import graphene.relay
 from graphql.language import ast
+import json
 
 TYPE_FIELD_META = "_meta"  # Name of the meta field for a GraphQL object type class
 _TYPE_FIELD_SCHEMA = "__schema"  # Attribute name for the reference to the YAMS entity schema from a GraphQL object type
@@ -116,6 +117,24 @@
         raise NotImplementedError
 
 
+class ScalarJsonb(graphene.Scalar):
+    """
+    Graphene scalar type for the JsonB type
+    """
+
+    @staticmethod
+    def serialize(value):
+        return json.loads(value)
+
+    @staticmethod
+    def parse_literal(node):
+        raise NotImplementedError
+
+    @staticmethod
+    def parse_value(value):
+        raise NotImplementedError
+
+
 _PRIMITIVES = {
     "String": graphene.String,
     "Password": graphene.String,
@@ -130,7 +149,8 @@
     "Bytes": ScalarBytes,
     "BigInt": ScalarBigInt,
     "Decimal": ScalarDecimal,
-    "Interval": ScalarInterval
+    "Interval": ScalarInterval,
+    "Jsonb": ScalarJsonb
 }