mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 09:36:44 +03:00
Improved type resolver. Refactored django utils
This commit is contained in:
parent
f414d4d55a
commit
2900ed66ad
|
@ -7,28 +7,7 @@ from graphene.core.fields import Field, LazyField
|
|||
from graphene.utils import cached_property, memoize, LazyMap
|
||||
|
||||
from graphene.relay.types import BaseNode
|
||||
|
||||
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
|
||||
from graphene.contrib.django.utils import get_type_for_model, lazy_map
|
||||
|
||||
|
||||
class DjangoConnectionField(relay.ConnectionField):
|
||||
|
|
23
graphene/contrib/django/utils.py
Normal file
23
graphene/contrib/django/utils.py
Normal 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
|
|
@ -139,9 +139,14 @@ class BaseObjectType(object):
|
|||
return resolve_fn(args, info)
|
||||
return self.get_field(field_name)
|
||||
|
||||
@classmethod
|
||||
def resolve_objecttype(cls, schema, instance, *_):
|
||||
return instance
|
||||
|
||||
@classmethod
|
||||
def resolve_type(cls, schema, instance, *_):
|
||||
return instance.internal_type(schema)
|
||||
objecttype = cls.resolve_objecttype(schema, instance, *_)
|
||||
return objecttype.internal_type(schema)
|
||||
|
||||
@classmethod
|
||||
@memoize
|
||||
|
|
Loading…
Reference in New Issue
Block a user