Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connections ignoring hints #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion graphene_django_optimizer/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ def _optimize_field_by_name(self, store, model, selection, field_def):
return False

def _get_optimization_hints(self, resolver):
return getattr(resolver, 'optimization_hints', None)
hints = getattr(resolver, 'optimization_hints', None)
if not hints and isinstance(resolver, functools.partial):
return self._get_optimization_hints(resolver.args[0])
return hints

def _get_value(self, info, value):
if isinstance(value, Variable):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_relay.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from django.db.models import Prefetch

import graphene_django_optimizer as gql_optimizer

Expand Down Expand Up @@ -175,3 +176,36 @@ def test_should_resolve_nested_variables():
child_edges = item_edges[0]['node']['relayAllChildren']['edges'][0]
assert len(child_edges) == 1
assert child_edges['node']['id'] == '8'


@pytest.mark.django_db
def test_should_optimize_hints_on_related_relay_connections():
item_1 = Item.objects.create(id=9, name='foo')
item_1.children.create(id=10, name='bar')
variables = {'itemsFirst': 1, 'childrenFirst': 1}
query = '''
query Query($itemsFirst: Int!, $childrenFirst: Int!) {
relayItems(first: $itemsFirst) {
edges {
node {
relayAllChildren(first: $childrenFirst) {
edges {
node {
id
}
}
}
}
}
}
}
'''
info = create_resolve_info(schema, query, variables=variables)

qs = Item.objects.all()
items = gql_optimizer.query(qs, info)
optimized_items = qs.prefetch_related(Prefetch(
'children',
Item.objects.all().only('id', 'parent_id'),
))
assert_query_equality(items, optimized_items)