mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Added LazyType
This commit is contained in:
parent
6ad668fe38
commit
b474010060
|
@ -36,8 +36,7 @@ class Schema(object):
|
|||
def T(self, object_type):
|
||||
if not object_type:
|
||||
return
|
||||
# if inspect.isclass(object_type) and issubclass(object_type, BaseType):
|
||||
if True:
|
||||
if inspect.isclass(object_type) and issubclass(object_type, BaseType) or isinstance(object_type, BaseType):
|
||||
if object_type not in self._types:
|
||||
internal_type = object_type.internal_type(self)
|
||||
self._types[object_type] = internal_type
|
||||
|
|
|
@ -7,6 +7,15 @@ class BaseType(object):
|
|||
return getattr(cls, 'T', None)
|
||||
|
||||
|
||||
class LazyType(BaseType):
|
||||
def __init__(self, type_str):
|
||||
self.type_str = type_str
|
||||
|
||||
def internal_type(self, schema):
|
||||
type = schema.get_type(self.type_str)
|
||||
return schema.T(type)
|
||||
|
||||
|
||||
@total_ordering
|
||||
class OrderedType(BaseType):
|
||||
creation_counter = 0
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import six
|
||||
from collections import OrderedDict
|
||||
from functools import wraps
|
||||
|
||||
from graphql.core.type import GraphQLField, GraphQLInputObjectField
|
||||
|
||||
from .base import OrderedType
|
||||
from .base import LazyType, OrderedType
|
||||
from .argument import to_arguments
|
||||
from ...utils import to_camel_case
|
||||
from ..types import BaseObjectType, InputObjectType
|
||||
|
@ -18,6 +19,8 @@ class Field(OrderedType):
|
|||
_creation_counter = kwargs.pop('_creation_counter', None)
|
||||
super(Field, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
if isinstance(type, six.string_types) and type != 'self':
|
||||
type = LazyType(type)
|
||||
self.type = type
|
||||
self.description = description
|
||||
args = OrderedDict(args or {}, **kwargs)
|
||||
|
|
|
@ -2,6 +2,7 @@ from graphql.core.type import GraphQLField, GraphQLInputObjectField, GraphQLStri
|
|||
|
||||
from ..field import Field, InputField
|
||||
from ..scalars import String
|
||||
from ..base import LazyType
|
||||
from graphene.core.types import ObjectType, InputObjectType
|
||||
from graphene.core.schema import Schema
|
||||
|
||||
|
@ -52,6 +53,25 @@ def test_field_custom_name():
|
|||
assert field.attname == 'my_field'
|
||||
|
||||
|
||||
def test_field_self():
|
||||
field = Field('self', name='my_customName')
|
||||
|
||||
class MyObjectType(ObjectType):
|
||||
my_field = field
|
||||
|
||||
assert field.type == MyObjectType
|
||||
|
||||
|
||||
def test_field_string_reference():
|
||||
field = Field('MyObjectType', name='my_customName')
|
||||
|
||||
class MyObjectType(ObjectType):
|
||||
my_field = field
|
||||
|
||||
assert isinstance(field.type, LazyType)
|
||||
assert field.type.type_str == 'MyObjectType'
|
||||
|
||||
|
||||
def test_field_custom_arguments():
|
||||
field = Field(None, name='my_customName', p=String())
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user