mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 01:26:49 +03:00
Initial optimize in connections
This commit is contained in:
parent
6e3fe9c40f
commit
25d680fa00
|
@ -4,6 +4,7 @@ from ...core.types.base import FieldType
|
|||
from ...core.types.definitions import List
|
||||
from ...relay import ConnectionField
|
||||
from ...relay.utils import is_node
|
||||
from .fetcher import get_fields
|
||||
from .utils import DJANGO_FILTER_INSTALLED, get_type_for_model, maybe_queryset
|
||||
|
||||
|
||||
|
@ -37,10 +38,14 @@ class DjangoConnectionField(DjangoField, ConnectionField):
|
|||
def get_queryset(self, resolved_qs, args, info):
|
||||
return resolved_qs
|
||||
|
||||
def get_fetch_fields(self, info):
|
||||
return get_fields(info)
|
||||
|
||||
def from_list(self, connection_type, resolved, args, info):
|
||||
if not resolved:
|
||||
resolved = self.get_manager()
|
||||
resolved_qs = maybe_queryset(resolved)
|
||||
only = self.get_fetch_fields(info)
|
||||
resolved_qs = maybe_queryset(resolved, only)
|
||||
qs = self.get_queryset(resolved_qs, args, info)
|
||||
return super(DjangoConnectionField, self).from_list(connection_type, qs, args, info)
|
||||
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import pytest
|
||||
|
||||
import graphene
|
||||
from graphene.contrib.django import DjangoObjectType
|
||||
from graphene.contrib.django import DjangoNode
|
||||
|
||||
from ..tests.models import Reporter
|
||||
from ..debug.plugin import DjangoDebugPlugin
|
||||
from ..fetcher import fetch_only_required, get_fields
|
||||
from ..fields import DjangoConnectionField
|
||||
|
||||
# from examples.starwars_django.models import Character
|
||||
|
||||
|
@ -18,7 +19,7 @@ def test_should_query_well():
|
|||
r2 = Reporter(last_name='Griffin')
|
||||
r2.save()
|
||||
|
||||
class ReporterType(DjangoObjectType):
|
||||
class ReporterType(DjangoNode):
|
||||
|
||||
class Meta:
|
||||
model = Reporter
|
||||
|
@ -26,6 +27,7 @@ def test_should_query_well():
|
|||
class Query(graphene.ObjectType):
|
||||
reporter = graphene.Field(ReporterType)
|
||||
all_reporters = ReporterType.List()
|
||||
all_reporters_connection = DjangoConnectionField(ReporterType)
|
||||
|
||||
@fetch_only_required
|
||||
def resolve_all_reporters(self, args, info):
|
||||
|
@ -40,6 +42,14 @@ def test_should_query_well():
|
|||
lastName
|
||||
email
|
||||
}
|
||||
allReportersConnection(first:1) {
|
||||
edges {
|
||||
node {
|
||||
lastName
|
||||
email
|
||||
}
|
||||
}
|
||||
}
|
||||
reporter {
|
||||
email
|
||||
}
|
||||
|
@ -58,12 +68,24 @@ def test_should_query_well():
|
|||
'lastName': 'Griffin',
|
||||
'email': '',
|
||||
}],
|
||||
'allReportersConnection': {
|
||||
'edges': [{
|
||||
'node': {
|
||||
'lastName': 'ABA',
|
||||
'email': '',
|
||||
}
|
||||
}]
|
||||
},
|
||||
'reporter': {
|
||||
'email': ''
|
||||
},
|
||||
'__debug': {
|
||||
'sql': [{
|
||||
'rawSql': str(Reporter.objects.all().only('last_name', 'email').query)
|
||||
}, {
|
||||
'rawSql': 'SELECT COUNT(*) AS "__count" FROM "tests_reporter"'
|
||||
}, {
|
||||
'rawSql': str(Reporter.objects.all()[:1].query)
|
||||
}, {
|
||||
'rawSql': str(Reporter.objects.only('email').order_by('pk')[:1].query)
|
||||
}]
|
||||
|
|
|
@ -49,10 +49,12 @@ class WrappedQueryset(LazyList):
|
|||
return self._origin.count()
|
||||
|
||||
|
||||
def maybe_queryset(value):
|
||||
def maybe_queryset(value, only=[]):
|
||||
if isinstance(value, Manager):
|
||||
value = value.get_queryset()
|
||||
if isinstance(value, QuerySet):
|
||||
if only:
|
||||
value = value.only(*only)
|
||||
return WrappedQueryset(value)
|
||||
return value
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user