Added UUID type

This commit is contained in:
Syrus Akbary 2017-07-24 23:09:59 -07:00
parent ed4bcce0cf
commit eabb9b202c
4 changed files with 69 additions and 0 deletions

View File

@ -27,6 +27,8 @@ if not __SETUP__:
Schema,
Scalar,
String, ID, Int, Float, Boolean,
JSONString,
UUID,
List, NonNull,
Enum,
Argument,
@ -63,6 +65,8 @@ if not __SETUP__:
'Float',
'Enum',
'Boolean',
'JSONString',
'UUID',
'List',
'NonNull',
'Argument',

View File

@ -5,6 +5,8 @@ from .objecttype import ObjectType
from .interface import Interface
from .mutation import Mutation
from .scalars import Scalar, String, ID, Int, Float, Boolean
from .json import JSONString
from .uuid import UUID
from .schema import Schema
from .structures import List, NonNull
from .enum import Enum
@ -34,6 +36,8 @@ __all__ = [
'ID',
'Int',
'Float',
'JSONString',
'UUID',
'Boolean',
'List',
'NonNull',

View File

@ -0,0 +1,34 @@
from ..uuid import UUID
from ..objecttype import ObjectType
from ..schema import Schema
class Query(ObjectType):
uuid = UUID(input=UUID())
def resolve_uuid(self, input):
return input
schema = Schema(query=Query)
def test_uuidstring_query():
uuid_value = 'dfeb3bcf-70fd-11e7-a61a-6003088f8204'
result = schema.execute('''{ uuid(input: "%s") }''' % uuid_value)
assert not result.errors
assert result.data == {
'uuid': uuid_value
}
def test_uuidstring_query_variable():
uuid_value = 'dfeb3bcf-70fd-11e7-a61a-6003088f8204'
result = schema.execute(
'''query Test($uuid: UUID){ uuid(input: $uuid) }''',
variable_values={'uuid': uuid_value}
)
assert not result.errors
assert result.data == {
'uuid': uuid_value
}

27
graphene/types/uuid.py Normal file
View File

@ -0,0 +1,27 @@
from __future__ import absolute_import
from uuid import UUID as _UUID
from graphql.language import ast
from .scalars import Scalar
class UUID(Scalar):
'''UUID'''
@staticmethod
def serialize(uuid):
if isinstance(uuid, str):
uuid = _UUID(uuid)
assert isinstance(uuid, _UUID), "Expected UUID instance, received {}".format(uuid)
return str(uuid)
@staticmethod
def parse_literal(node):
if isinstance(node, ast.StringValue):
return _UUID(node.value)
@staticmethod
def parse_value(value):
return _UUID(value)