Added JSONString custom scalar

This commit is contained in:
Syrus Akbary 2016-04-02 19:19:40 -07:00
parent ca0d1a3d7a
commit 81560df6a1
4 changed files with 44 additions and 9 deletions

View File

@ -1,9 +1,8 @@
from django.db import models
from ...core.classtypes.objecttype import ObjectType
from ...core.types.definitions import List
from ...core.types.field import Field
from ...core.types.scalars import ID, Boolean, Float, Int, String
from ...core.types.custom_scalars import JSONString
from ...core.classtypes.enum import Enum
from .compat import RelatedObject, UUIDField, ArrayField, HStoreField, JSONField, RangeField
from .utils import get_related_model, import_single_dispatch
@ -103,7 +102,7 @@ def convert_postgres_array_to_list(field):
@convert_django_field.register(HStoreField)
@convert_django_field.register(JSONField)
def convert_posgres_field_to_string(field):
return String(description=field.help_text)
return JSONString(description=field.help_text)
@convert_django_field.register(RangeField)

View File

@ -8,6 +8,7 @@ from ..converter import (
from ..fields import (ConnectionOrListField,
DjangoModelField)
from ..compat import MissingType, ArrayField, HStoreField, JSONField, RangeField
from graphene.core.types.custom_scalars import JSONString
from .models import Article, Reporter
@ -168,13 +169,13 @@ def test_should_postgres_array_multiple_convert_list():
@pytest.mark.skipif(HStoreField is MissingType,
reason="HStoreField should exist")
def test_should_postgres_hstore_convert_string():
assert_conversion(HStoreField, graphene.String)
assert_conversion(HStoreField, JSONString)
@pytest.mark.skipif(JSONField is MissingType,
reason="JSONField should exist")
def test_should_postgres_json_convert_string():
assert_conversion(JSONField, graphene.String)
assert_conversion(JSONField, JSONString)
@pytest.mark.skipif(RangeField is MissingType,

View File

@ -66,11 +66,14 @@ def test_should_query_well():
@pytest.mark.skipif(RangeField is MissingType,
reason="RangeField should exist")
def test_should_query_ranges():
from django.contrib.postgres.fields import IntegerRangeField
def test_should_query_postgres_fields():
from django.contrib.postgres.fields import IntegerRangeField, ArrayField, JSONField, HStoreField
class Event(models.Model):
ages = IntegerRangeField(help_text='Range desc')
ages = IntegerRangeField(help_text='The age ranges')
data = JSONField(help_text='Data')
store = HStoreField()
tags = ArrayField(models.CharField(max_length=50))
class EventType(DjangoObjectType):
class Meta:
@ -80,19 +83,30 @@ def test_should_query_ranges():
event = graphene.Field(EventType)
def resolve_event(self, *args, **kwargs):
return Event(ages=(0, 10))
return Event(
ages=(0, 10),
data={'angry_babies': True},
store={'h': 'store'},
tags=['child', 'angry', 'babies']
)
schema = graphene.Schema(query=Query)
query = '''
query myQuery {
event {
ages
tags
data
store
}
}
'''
expected = {
'event': {
'ages': [0, 10],
'tags': ['child', 'angry', 'babies'],
'data': '{"angry_babies": true}',
'store': '{"h": "store"}',
},
}
result = schema.execute(query)

View File

@ -0,0 +1,21 @@
import json
from graphql.core.language import ast
from ...core.classtypes.scalar import Scalar
class JSONString(Scalar):
'''JSON String'''
@staticmethod
def serialize(dt):
return json.dumps(dt)
@staticmethod
def parse_literal(node):
if isinstance(node, ast.StringValue):
return json.dumps(node.value)
@staticmethod
def parse_value(value):
return json.dumps(value)