Skip to content
Snippets Groups Projects
Unverified Commit 1aa787b1 authored by Tomás Fox's avatar Tomás Fox Committed by GitHub
Browse files

Merge pull request #6 from maarcingebala/fix-resolving-variable-values

Fix resolving nested variables. Fixes #5
parents ce864bf7 6b5b6a09
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ from graphql.execution.base import (
from graphql.language.ast import (
FragmentSpread,
InlineFragment,
Variable
)
from graphql.type.definition import (
GraphQLInterfaceType,
......@@ -180,7 +181,17 @@ class QueryOptimizer(object):
self._get_type(field_def),
parent_type,
)
args = tuple(arg.value.value for arg in selection.arguments)
args = []
for arg in selection.arguments:
if isinstance(arg.value, Variable):
var_name = arg.value.name.value
value = info.variable_values.get(var_name)
else:
value = arg.value.value
args.append(value)
args = tuple(args)
self._add_optimization_hints(
optimization_hints.select_related(info, *args),
store.select_list,
......@@ -336,7 +347,6 @@ def _get_path_from_parent(self, parent):
chain.append(model)
# Construct a list of the PathInfos between models in chain.
path = []
import ipdb; ipdb.set_trace()
for i, ancestor in enumerate(chain[:-1]):
child = chain[i + 1]
link = child._meta.get_ancestor_link(ancestor)
......
......@@ -64,6 +64,10 @@ class BaseItemType(DjangoObjectType):
graphene.Field('tests.schema.ItemType'),
model_field='parent',
)
relay_all_children = gql_optimizer.field(
DjangoConnectionField('tests.schema.ItemNode'),
model_field='children'
)
class Meta:
model = Item
......
......@@ -145,3 +145,28 @@ def test_should_work_fine_with_page_info_field_below_edges_field_when_only_optim
''')
assert not result.errors
assert result.data['relayItems']['pageInfo']['hasNextPage'] is True
@pytest.mark.django_db
def test_resolve_nested_variables():
item_1 = Item.objects.create(id=7, name='foo')
item_1.children.create(id=8, name='bar')
variables = {'items_first': 1, 'schema_first': 1}
result = schema.execute('''
query Query($itemsFirst: Int, $childrenFirst: Int) {
relayItems(first: $itemsFirst) {
edges {
node {
relayAllChildren(first: $childrenFirst) {
edges {
node {
id
}
}
}
}
}
}
}
''', variables=variables)
assert not result.errors
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment