Improved ScalarType

This commit is contained in:
Syrus Akbary 2016-06-13 18:55:50 -07:00
parent 8078937359
commit 7219babbd7
3 changed files with 21 additions and 27 deletions

View File

@ -1,29 +1,37 @@
import six import six
from graphql import GraphQLScalarType, GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID from graphql import GraphQLScalarType, GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID
from .definitions import ClassTypeMeta, GrapheneGraphQLType from .definitions import GrapheneGraphQLType
from .unmountedtype import UnmountedType from .unmountedtype import UnmountedType
from .options import Options
from ..utils.is_base_type import is_base_type
class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType): class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType):
pass pass
class ScalarTypeMeta(ClassTypeMeta): class ScalarTypeMeta(type):
def get_options(cls, meta): def __new__(cls, name, bases, attrs):
return cls.options_class( super_new = super(ScalarTypeMeta, cls).__new__
meta,
# Also ensure initialization is only performed for subclasses of Model
# (excluding Model class itself).
if not is_base_type(bases, ScalarTypeMeta):
return super_new(cls, name, bases, attrs)
options = Options(
attrs.pop('Meta', None),
name=None, name=None,
description=None, description=None,
graphql_type=None, graphql_type=None
abstract=False
) )
def construct(cls, *args, **kwargs): cls = super_new(cls, name, bases, dict(attrs, _meta=options))
constructed = super(ScalarTypeMeta, cls).construct(*args, **kwargs)
if not cls._meta.graphql_type and not cls._meta.abstract: if not options.graphql_type:
cls._meta.graphql_type = GrapheneScalarType( options.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 or cls.__doc__, description=cls._meta.description or cls.__doc__,
@ -33,12 +41,11 @@ class ScalarTypeMeta(ClassTypeMeta):
parse_literal=getattr(cls, 'parse_literal', None), parse_literal=getattr(cls, 'parse_literal', None),
) )
return constructed return cls
class Scalar(six.with_metaclass(ScalarTypeMeta, UnmountedType)): class Scalar(six.with_metaclass(ScalarTypeMeta, UnmountedType)):
class Meta: pass
abstract = True
def construct_scalar_class(graphql_type): def construct_scalar_class(graphql_type):

View File

@ -7,8 +7,6 @@ def get_graphql_type(_type):
if is_type(_type): if is_type(_type):
return _type return _type
elif is_graphene_type(_type): elif is_graphene_type(_type):
if _type._meta.abstract:
raise Exception("{} has no type. Only non abstract types have GraphQL type.".format(_type.__name__))
return _type._meta.graphql_type return _type._meta.graphql_type
raise Exception("Cannot get GraphQL type of {}.".format(_type)) raise Exception("Cannot get GraphQL type of {}.".format(_type))

View File

@ -19,17 +19,6 @@ def test_get_graphql_type_graphene():
assert is_type(get_graphql_type(MyGrapheneType)) assert is_type(get_graphql_type(MyGrapheneType))
def test_get_graphql_type_graphene_abstract():
class MyGrapheneType(ObjectType):
class Meta:
abstract = True
with pytest.raises(Exception) as excinfo:
get_graphql_type(MyGrapheneType)
assert "MyGrapheneType has no type. Only non abstract types have GraphQL type." == str(excinfo.value)
def test_get_graphql_type_custom_graphene_type(): def test_get_graphql_type_custom_graphene_type():
class MyGrapheneType(ObjectType): class MyGrapheneType(ObjectType):
class Meta: class Meta: