Skip to content

Commit 5e708cc

Browse files
committed
Improved tests. Improved schema.type getter. Remove duplicated Scalar code
1 parent afdddaf commit 5e708cc

File tree

6 files changed

+19
-65
lines changed

6 files changed

+19
-65
lines changed

graphene/core/classtypes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import copy
22
import inspect
3-
from functools import partial
43
from collections import OrderedDict
4+
from functools import partial
55

66
import six
77

graphene/core/schema.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,20 @@ def __init__(self, query=None, mutation=None, subscription=None,
3838
def __repr__(self):
3939
return '<Schema: %s (%s)>' % (str(self.name), hash(self))
4040

41-
def T(self, object_type):
42-
if not object_type:
41+
def T(self, _type):
42+
if not _type:
4343
return
44-
if inspect.isclass(object_type) and issubclass(
45-
object_type, (BaseType, ClassType)) or isinstance(
46-
object_type, BaseType):
47-
if object_type not in self._types:
48-
internal_type = object_type.internal_type(self)
49-
self._types[object_type] = internal_type
50-
is_objecttype = inspect.isclass(
51-
object_type) and issubclass(object_type, ClassType)
52-
if is_objecttype:
53-
self.register(object_type)
54-
return self._types[object_type]
44+
is_classtype = inspect.isclass(_type) and issubclass(_type, ClassType)
45+
is_instancetype = isinstance(_type, BaseType)
46+
if is_classtype or is_instancetype:
47+
if _type not in self._types:
48+
internal_type = _type.internal_type(self)
49+
self._types[_type] = internal_type
50+
if is_classtype:
51+
self.register(_type)
52+
return self._types[_type]
5553
else:
56-
return object_type
54+
return _type
5755

5856
@property
5957
def executor(self):

graphene/core/types/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Compatibility import
55
from .objecttype import Interface, ObjectType, Mutation, InputObjectType
66

7-
from .scalars import String, ID, Boolean, Int, Float, Scalar
7+
from .scalars import String, ID, Boolean, Int, Float
88
from .field import Field, InputField
99

1010
__all__ = [
@@ -26,5 +26,4 @@
2626
'ID',
2727
'Boolean',
2828
'Int',
29-
'Float',
30-
'Scalar']
29+
'Float']

graphene/core/types/scalars.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID,
2-
GraphQLInt, GraphQLScalarType, GraphQLString)
2+
GraphQLInt, GraphQLString)
33

44
from .base import MountedType
55

@@ -22,20 +22,3 @@ class ID(MountedType):
2222

2323
class Float(MountedType):
2424
T = GraphQLFloat
25-
26-
27-
class Scalar(MountedType):
28-
29-
@classmethod
30-
def internal_type(cls, schema):
31-
serialize = getattr(cls, 'serialize')
32-
parse_literal = getattr(cls, 'parse_literal')
33-
parse_value = getattr(cls, 'parse_value')
34-
35-
return GraphQLScalarType(
36-
name=cls.__name__,
37-
description=cls.__doc__,
38-
serialize=serialize,
39-
parse_value=parse_value,
40-
parse_literal=parse_literal
41-
)

graphene/core/types/tests/test_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
def test_field_internal_type():
1414
resolver = lambda *args: 'RESOLVED'
1515

16-
field = Field(String, description='My argument', resolver=resolver)
16+
field = Field(String(), description='My argument', resolver=resolver)
1717

1818
class Query(ObjectType):
1919
my_field = field
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from graphql.core.type import (GraphQLBoolean, GraphQLFloat, GraphQLID,
2-
GraphQLInt, GraphQLScalarType, GraphQLString)
2+
GraphQLInt, GraphQLString)
33

44
from graphene.core.schema import Schema
55

6-
from ..scalars import ID, Boolean, Float, Int, Scalar, String
6+
from ..scalars import ID, Boolean, Float, Int, String
77

88
schema = Schema()
99

@@ -26,29 +26,3 @@ def test_id_scalar():
2626

2727
def test_float_scalar():
2828
assert schema.T(Float()) == GraphQLFloat
29-
30-
31-
def test_custom_scalar():
32-
import datetime
33-
from graphql.core.language import ast
34-
35-
class DateTimeScalar(Scalar):
36-
'''DateTimeScalar Documentation'''
37-
@staticmethod
38-
def serialize(dt):
39-
return dt.isoformat()
40-
41-
@staticmethod
42-
def parse_literal(node):
43-
if isinstance(node, ast.StringValue):
44-
return datetime.datetime.strptime(
45-
node.value, "%Y-%m-%dT%H:%M:%S.%f")
46-
47-
@staticmethod
48-
def parse_value(value):
49-
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
50-
51-
scalar_type = schema.T(DateTimeScalar)
52-
assert isinstance(scalar_type, GraphQLScalarType)
53-
assert scalar_type.name == 'DateTimeScalar'
54-
assert scalar_type.description == 'DateTimeScalar Documentation'

0 commit comments

Comments
 (0)