Improved type resolver. Refactored django utils

This commit is contained in:
Syrus Akbary 2015-10-15 19:13:38 -07:00
parent f414d4d55a
commit 2900ed66ad
3 changed files with 30 additions and 23 deletions

View File

@ -7,28 +7,7 @@ from graphene.core.fields import Field, LazyField
from graphene.utils import cached_property, memoize, LazyMap from graphene.utils import cached_property, memoize, LazyMap
from graphene.relay.types import BaseNode from graphene.relay.types import BaseNode
from graphene.contrib.django.utils import get_type_for_model, lazy_map
from django.db.models.query import QuerySet
from django.db.models.manager import Manager
@memoize
def get_type_for_model(schema, model):
schema = schema
types = schema.types.values()
for _type in types:
type_model = hasattr(_type, '_meta') and getattr(
_type._meta, 'model', None)
if model == type_model:
return _type
def lazy_map(value, func):
if isinstance(value, Manager):
value = value.get_queryset()
if isinstance(value, QuerySet):
return LazyMap(value, func)
return value
class DjangoConnectionField(relay.ConnectionField): class DjangoConnectionField(relay.ConnectionField):

View File

@ -0,0 +1,23 @@
from django.db.models.query import QuerySet
from django.db.models.manager import Manager
from graphene.utils import memoize, LazyMap
# @memoize
def get_type_for_model(schema, model):
schema = schema
types = schema.types.values()
for _type in types:
type_model = hasattr(_type, '_meta') and getattr(
_type._meta, 'model', None)
if model == type_model:
return _type
def lazy_map(value, func):
if isinstance(value, Manager):
value = value.get_queryset()
if isinstance(value, QuerySet):
return LazyMap(value, func)
return value

View File

@ -139,9 +139,14 @@ class BaseObjectType(object):
return resolve_fn(args, info) return resolve_fn(args, info)
return self.get_field(field_name) return self.get_field(field_name)
@classmethod
def resolve_objecttype(cls, schema, instance, *_):
return instance
@classmethod @classmethod
def resolve_type(cls, schema, instance, *_): def resolve_type(cls, schema, instance, *_):
return instance.internal_type(schema) objecttype = cls.resolve_objecttype(schema, instance, *_)
return objecttype.internal_type(schema)
@classmethod @classmethod
@memoize @memoize