Skip to content

Commit b474010

Browse files
committed
Added LazyType
1 parent 6ad668f commit b474010

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

graphene/core/schema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def __repr__(self):
3636
def T(self, object_type):
3737
if not object_type:
3838
return
39-
# if inspect.isclass(object_type) and issubclass(object_type, BaseType):
40-
if True:
39+
if inspect.isclass(object_type) and issubclass(object_type, BaseType) or isinstance(object_type, BaseType):
4140
if object_type not in self._types:
4241
internal_type = object_type.internal_type(self)
4342
self._types[object_type] = internal_type

graphene/core/types/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ def internal_type(cls, schema):
77
return getattr(cls, 'T', None)
88

99

10+
class LazyType(BaseType):
11+
def __init__(self, type_str):
12+
self.type_str = type_str
13+
14+
def internal_type(self, schema):
15+
type = schema.get_type(self.type_str)
16+
return schema.T(type)
17+
18+
1019
@total_ordering
1120
class OrderedType(BaseType):
1221
creation_counter = 0

graphene/core/types/field.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import six
12
from collections import OrderedDict
23
from functools import wraps
34

45
from graphql.core.type import GraphQLField, GraphQLInputObjectField
56

6-
from .base import OrderedType
7+
from .base import LazyType, OrderedType
78
from .argument import to_arguments
89
from ...utils import to_camel_case
910
from ..types import BaseObjectType, InputObjectType
@@ -18,6 +19,8 @@ def __init__(self, type, description=None, args=None, name=None, resolver=None,
1819
_creation_counter = kwargs.pop('_creation_counter', None)
1920
super(Field, self).__init__(_creation_counter=_creation_counter)
2021
self.name = name
22+
if isinstance(type, six.string_types) and type != 'self':
23+
type = LazyType(type)
2124
self.type = type
2225
self.description = description
2326
args = OrderedDict(args or {}, **kwargs)

graphene/core/types/tests/test_field.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from ..field import Field, InputField
44
from ..scalars import String
5+
from ..base import LazyType
56
from graphene.core.types import ObjectType, InputObjectType
67
from graphene.core.schema import Schema
78

@@ -52,6 +53,25 @@ class MyObjectType(ObjectType):
5253
assert field.attname == 'my_field'
5354

5455

56+
def test_field_self():
57+
field = Field('self', name='my_customName')
58+
59+
class MyObjectType(ObjectType):
60+
my_field = field
61+
62+
assert field.type == MyObjectType
63+
64+
65+
def test_field_string_reference():
66+
field = Field('MyObjectType', name='my_customName')
67+
68+
class MyObjectType(ObjectType):
69+
my_field = field
70+
71+
assert isinstance(field.type, LazyType)
72+
assert field.type.type_str == 'MyObjectType'
73+
74+
5575
def test_field_custom_arguments():
5676
field = Field(None, name='my_customName', p=String())
5777

0 commit comments

Comments
 (0)