mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Improved tests. Improved schema.type getter. Remove duplicated Scalar code
This commit is contained in:
parent
afdddafb3d
commit
5e708cc919
|
@ -1,7 +1,7 @@
|
||||||
import copy
|
import copy
|
||||||
import inspect
|
import inspect
|
||||||
from functools import partial
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
|
|
@ -38,22 +38,20 @@ class Schema(object):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
|
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
|
||||||
|
|
||||||
def T(self, object_type):
|
def T(self, _type):
|
||||||
if not object_type:
|
if not _type:
|
||||||
return
|
return
|
||||||
if inspect.isclass(object_type) and issubclass(
|
is_classtype = inspect.isclass(_type) and issubclass(_type, ClassType)
|
||||||
object_type, (BaseType, ClassType)) or isinstance(
|
is_instancetype = isinstance(_type, BaseType)
|
||||||
object_type, BaseType):
|
if is_classtype or is_instancetype:
|
||||||
if object_type not in self._types:
|
if _type not in self._types:
|
||||||
internal_type = object_type.internal_type(self)
|
internal_type = _type.internal_type(self)
|
||||||
self._types[object_type] = internal_type
|
self._types[_type] = internal_type
|
||||||
is_objecttype = inspect.isclass(
|
if is_classtype:
|
||||||
object_type) and issubclass(object_type, ClassType)
|
self.register(_type)
|
||||||
if is_objecttype:
|
return self._types[_type]
|
||||||
self.register(object_type)
|
|
||||||
return self._types[object_type]
|
|
||||||
else:
|
else:
|
||||||
return object_type
|
return _type
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def executor(self):
|
def executor(self):
|
||||||
|
|
|
@ -4,7 +4,7 @@ from .definitions import List, NonNull
|
||||||
# Compatibility import
|
# Compatibility import
|
||||||
from .objecttype import Interface, ObjectType, Mutation, InputObjectType
|
from .objecttype import Interface, ObjectType, Mutation, InputObjectType
|
||||||
|
|
||||||
from .scalars import String, ID, Boolean, Int, Float, Scalar
|
from .scalars import String, ID, Boolean, Int, Float
|
||||||
from .field import Field, InputField
|
from .field import Field, InputField
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
@ -26,5 +26,4 @@ __all__ = [
|
||||||
'ID',
|
'ID',
|
||||||
'Boolean',
|
'Boolean',
|
||||||
'Int',
|
'Int',
|
||||||
'Float',
|
'Float']
|
||||||
'Scalar']
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID,
|
from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID,
|
||||||
GraphQLInt, GraphQLScalarType, GraphQLString)
|
GraphQLInt, GraphQLString)
|
||||||
|
|
||||||
from .base import MountedType
|
from .base import MountedType
|
||||||
|
|
||||||
|
@ -22,20 +22,3 @@ class ID(MountedType):
|
||||||
|
|
||||||
class Float(MountedType):
|
class Float(MountedType):
|
||||||
T = GraphQLFloat
|
T = GraphQLFloat
|
||||||
|
|
||||||
|
|
||||||
class Scalar(MountedType):
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def internal_type(cls, schema):
|
|
||||||
serialize = getattr(cls, 'serialize')
|
|
||||||
parse_literal = getattr(cls, 'parse_literal')
|
|
||||||
parse_value = getattr(cls, 'parse_value')
|
|
||||||
|
|
||||||
return GraphQLScalarType(
|
|
||||||
name=cls.__name__,
|
|
||||||
description=cls.__doc__,
|
|
||||||
serialize=serialize,
|
|
||||||
parse_value=parse_value,
|
|
||||||
parse_literal=parse_literal
|
|
||||||
)
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ from ..scalars import String
|
||||||
def test_field_internal_type():
|
def test_field_internal_type():
|
||||||
resolver = lambda *args: 'RESOLVED'
|
resolver = lambda *args: 'RESOLVED'
|
||||||
|
|
||||||
field = Field(String, description='My argument', resolver=resolver)
|
field = Field(String(), description='My argument', resolver=resolver)
|
||||||
|
|
||||||
class Query(ObjectType):
|
class Query(ObjectType):
|
||||||
my_field = field
|
my_field = field
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID,
|
from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID,
|
||||||
GraphQLInt, GraphQLScalarType, GraphQLString)
|
GraphQLInt, GraphQLString)
|
||||||
|
|
||||||
from graphene.core.schema import Schema
|
from graphene.core.schema import Schema
|
||||||
|
|
||||||
from ..scalars import ID, Boolean, Float, Int, Scalar, String
|
from ..scalars import ID, Boolean, Float, Int, String
|
||||||
|
|
||||||
schema = Schema()
|
schema = Schema()
|
||||||
|
|
||||||
|
@ -26,29 +26,3 @@ def test_id_scalar():
|
||||||
|
|
||||||
def test_float_scalar():
|
def test_float_scalar():
|
||||||
assert schema.T(Float()) == GraphQLFloat
|
assert schema.T(Float()) == GraphQLFloat
|
||||||
|
|
||||||
|
|
||||||
def test_custom_scalar():
|
|
||||||
import datetime
|
|
||||||
from graphql.core.language import ast
|
|
||||||
|
|
||||||
class DateTimeScalar(Scalar):
|
|
||||||
'''DateTimeScalar Documentation'''
|
|
||||||
@staticmethod
|
|
||||||
def serialize(dt):
|
|
||||||
return dt.isoformat()
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def parse_literal(node):
|
|
||||||
if isinstance(node, ast.StringValue):
|
|
||||||
return datetime.datetime.strptime(
|
|
||||||
node.value, "%Y-%m-%dT%H:%M:%S.%f")
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def parse_value(value):
|
|
||||||
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
|
|
||||||
|
|
||||||
scalar_type = schema.T(DateTimeScalar)
|
|
||||||
assert isinstance(scalar_type, GraphQLScalarType)
|
|
||||||
assert scalar_type.name == 'DateTimeScalar'
|
|
||||||
assert scalar_type.description == 'DateTimeScalar Documentation'
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user