graphene/graphene-django/graphene_django/fields.py

50 lines
1.6 KiB
Python
Raw Normal View History

from django.db.models.query import QuerySet
from graphene.relay import ConnectionField
from graphql_relay.connection.arrayconnection import connection_from_list_slice
2016-06-22 09:04:12 +03:00
from .utils import maybe_queryset, DJANGO_FILTER_INSTALLED
class DjangoConnectionField(ConnectionField):
def __init__(self, *args, **kwargs):
self.on = kwargs.pop('on', False)
return super(DjangoConnectionField, self).__init__(*args, **kwargs)
@property
def model(self):
return self.connection._meta.node._meta.model
def get_manager(self):
if self.on:
return getattr(self.model, self.on)
else:
return self.model._default_manager
def default_resolver(self, root, args, context, info):
return getattr(root, self.source or self.attname, self.get_manager())
2016-06-24 08:41:22 +03:00
@staticmethod
def connection_resolver(resolver, connection, root, args, context, info):
iterable = resolver(root, args, context, info)
iterable = maybe_queryset(iterable)
if isinstance(iterable, QuerySet):
_len = iterable.count()
else:
_len = len(iterable)
return connection_from_list_slice(
iterable,
args,
slice_start=0,
list_length=_len,
list_slice_length=_len,
2016-06-24 08:41:22 +03:00
connection_type=connection,
edge_type=connection.Edge,
)
2016-06-22 09:04:12 +03:00
def get_connection_field(*args, **kwargs):
if DJANGO_FILTER_INSTALLED:
from .filter.fields import DjangoFilterConnectionField
return DjangoFilterConnectionField(*args, **kwargs)
return ConnectionField(*args, **kwargs)