mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-13 13:16:49 +03:00
Improved scalar implementation thinking immutable
This commit is contained in:
parent
77ec170dd5
commit
251d7106e2
|
@ -6,45 +6,7 @@ from .proxy import TypeProxy
|
||||||
|
|
||||||
|
|
||||||
class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType):
|
class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType):
|
||||||
|
pass
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
GrapheneGraphQLType.__init__(self, *args, **kwargs)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def default_parse(value):
|
|
||||||
return None
|
|
||||||
|
|
||||||
def setup(self):
|
|
||||||
serialize = getattr(self.graphene_type, 'serialize', None)
|
|
||||||
parse_value = getattr(self.graphene_type, 'parse_value', None)
|
|
||||||
parse_literal = getattr(self.graphene_type, 'parse_literal', None)
|
|
||||||
|
|
||||||
assert callable(serialize), (
|
|
||||||
'{} must provide "serialize" function. If this custom Scalar is '
|
|
||||||
'also used as an input type, ensure "parse_value" and "parse_literal" '
|
|
||||||
'functions are also provided.'
|
|
||||||
).format(self)
|
|
||||||
|
|
||||||
if parse_value is not None or parse_literal is not None:
|
|
||||||
assert callable(parse_value) and callable(parse_literal), (
|
|
||||||
'{} must provide both "parse_value" and "parse_literal" functions.'.format(self)
|
|
||||||
)
|
|
||||||
|
|
||||||
self._serialize = serialize
|
|
||||||
self._parse_value = parse_value
|
|
||||||
self._parse_literal = parse_literal
|
|
||||||
|
|
||||||
@property
|
|
||||||
def serialize(self):
|
|
||||||
return self.graphene_type.serialize
|
|
||||||
|
|
||||||
@property
|
|
||||||
def parse_value(self):
|
|
||||||
return getattr(self.graphene_type, 'parse_value', self.default_parse)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def parse_literal(self):
|
|
||||||
return getattr(self.graphene_type, 'parse_literal', self.default_parse)
|
|
||||||
|
|
||||||
|
|
||||||
class ScalarTypeMeta(ClassTypeMeta):
|
class ScalarTypeMeta(ClassTypeMeta):
|
||||||
|
@ -59,19 +21,21 @@ class ScalarTypeMeta(ClassTypeMeta):
|
||||||
)
|
)
|
||||||
|
|
||||||
def construct_graphql_type(cls, bases):
|
def construct_graphql_type(cls, bases):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def construct(cls, *args, **kwargs):
|
||||||
|
constructed = super(ScalarTypeMeta, cls).construct(*args, **kwargs)
|
||||||
if not cls._meta.graphql_type and not cls._meta.abstract:
|
if not cls._meta.graphql_type and not cls._meta.abstract:
|
||||||
cls._meta.graphql_type = GrapheneScalarType(
|
cls._meta.graphql_type = GrapheneScalarType(
|
||||||
graphene_type=cls,
|
graphene_type=cls,
|
||||||
name=cls._meta.name or cls.__name__,
|
name=cls._meta.name or cls.__name__,
|
||||||
description=cls._meta.description,
|
description=cls._meta.description,
|
||||||
# For passing the assertion in GraphQLScalarType
|
|
||||||
serialize=lambda: None
|
serialize=getattr(cls, 'serialize', None),
|
||||||
|
parse_value=getattr(cls, 'parse_value', None),
|
||||||
|
parse_literal=getattr(cls, 'parse_literal', None),
|
||||||
)
|
)
|
||||||
|
|
||||||
def construct(cls, *args, **kwargs):
|
|
||||||
constructed = super(ScalarTypeMeta, cls).construct(*args, **kwargs)
|
|
||||||
if isinstance(cls._meta.graphql_type, GrapheneScalarType):
|
|
||||||
cls._meta.graphql_type.setup()
|
|
||||||
return constructed
|
return constructed
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -48,8 +48,3 @@ def test_custom_scalar_empty():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
assert """DatetimeScalar must provide "serialize" function.""" in str(excinfo.value)
|
assert """DatetimeScalar must provide "serialize" function.""" in str(excinfo.value)
|
||||||
|
|
||||||
|
|
||||||
def test_custom_scalar():
|
|
||||||
graphql_type = DatetimeScalar._meta.graphql_type
|
|
||||||
assert graphql_type.serialize == DatetimeScalar.serialize
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user