Support deserialization of Json values in GraphQL queries
authorLaurent Wouters <lwouters@cenotelie.fr>
Fri, 14 Dec 2018 14:46:50 +0100
changeset 8 66fe6779aa70
parent 7 caf60851619d
child 9 fa1f617f137e
Support deserialization of Json values in GraphQL queries
cubicweb_graphql/gql_schema.py
cubicweb_graphql/utils.py
--- a/cubicweb_graphql/gql_schema.py	Thu Dec 13 15:15:55 2018 +0100
+++ b/cubicweb_graphql/gql_schema.py	Fri Dec 14 14:46:50 2018 +0100
@@ -24,6 +24,8 @@
 from graphql.language import ast
 import json
 
+import utils
+
 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
 FIELD_SUB_SEPARATOR = "_as_"  # Separator between a field name and its type in generated typed sub-fields (e.g. thing_as_Stuff)
@@ -128,7 +130,8 @@
 
     @staticmethod
     def parse_literal(node):
-        raise NotImplementedError
+        serialized = utils.serialize_value(node)
+        return json.loads(serialized)
 
     @staticmethod
     def parse_value(value):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cubicweb_graphql/utils.py	Fri Dec 14 14:46:50 2018 +0100
@@ -0,0 +1,50 @@
+# copyright 2003-2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+
+"""Utility APIs for the manipulation of GraphQL data types"""
+
+from graphql.language import ast
+
+
+def serialize_value(value):
+    """
+    Serializes an instance of graphql.language.ast.Value as a valid JSON string
+    :param value: The value to serialize
+    :return: The string of a valid JSON representation
+    """
+    if value is None:
+        return "null"
+    if isinstance(value, ast.StringValue):
+        return '"' + value.value.replace('"', '\\"') + '"'
+    elif isinstance(value, ast.IntValue):
+        return str(value.value)
+    elif isinstance(value, ast.FloatValue):
+        return str(value.value)
+    elif isinstance(value, ast.BooleanValue):
+        return str(value.value)
+    elif isinstance(value, ast.Variable):
+        raise NotImplementedError
+    elif isinstance(value, ast.EnumValue):
+        raise NotImplementedError
+    elif isinstance(value, ast.ListValue):
+        return "[" + ", ".join([serialize_value(x) for x in value.values]) + "]"
+    elif isinstance(value, ast.ObjectValue):
+        return "{" + ", ".join(
+            ["\"" + field.name.value + "\": " + serialize_value(field.value) for field in value.fields]) + "}"
+    else:
+        raise NotImplementedError