Make DjangoDebugContext wait for nested fields (#591)

* Make DjangoDebugContext wait for nested fields

This commit makes DjangoDebugContext wait for all field's promises,
even for fields that only started their resolvers after __debug was
resolved.

Fixes #293.

* Run format
This commit is contained in:
Tomasz Kontusz 2019-08-07 09:09:17 +02:00 committed by Jonathan Kim
parent 6e137da469
commit 11605dcdc6
2 changed files with 72 additions and 1 deletions

View File

@ -16,14 +16,18 @@ class DjangoDebugContext(object):
def get_debug_promise(self):
if not self.debug_promise:
self.debug_promise = Promise.all(self.promises)
self.promises = []
return self.debug_promise.then(self.on_resolve_all_promises)
def on_resolve_all_promises(self, values):
if self.promises:
self.debug_promise = None
return self.get_debug_promise()
self.disable_instrumentation()
return self.object
def add_promise(self, promise):
if self.debug_promise and not self.debug_promise.is_fulfilled:
if self.debug_promise:
self.promises.append(promise)
def enable_instrumentation(self):

View File

@ -60,6 +60,73 @@ def test_should_query_field():
assert result.data == expected
def test_should_query_nested_field():
r1 = Reporter(last_name="ABA")
r1.save()
r2 = Reporter(last_name="Griffin")
r2.save()
r2.pets.add(r1)
r1.pets.add(r2)
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class Query(graphene.ObjectType):
reporter = graphene.Field(ReporterType)
debug = graphene.Field(DjangoDebug, name="__debug")
def resolve_reporter(self, info, **args):
return Reporter.objects.first()
query = """
query ReporterQuery {
reporter {
lastName
pets { edges { node {
lastName
pets { edges { node { lastName } } }
} } }
}
__debug {
sql {
rawSql
}
}
}
"""
expected = {
"reporter": {
"lastName": "ABA",
"pets": {
"edges": [
{
"node": {
"lastName": "Griffin",
"pets": {"edges": [{"node": {"lastName": "ABA"}}]},
}
}
]
},
}
}
schema = graphene.Schema(query=Query)
result = schema.execute(
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
)
assert not result.errors
query = str(Reporter.objects.order_by("pk")[:1].query)
assert result.data["__debug"]["sql"][0]["rawSql"] == query
assert "COUNT" in result.data["__debug"]["sql"][1]["rawSql"]
assert "tests_reporter_pets" in result.data["__debug"]["sql"][2]["rawSql"]
assert "COUNT" in result.data["__debug"]["sql"][3]["rawSql"]
assert "tests_reporter_pets" in result.data["__debug"]["sql"][4]["rawSql"]
assert len(result.data["__debug"]["sql"]) == 5
assert result.data["reporter"] == expected["reporter"]
def test_should_query_list():
r1 = Reporter(last_name="ABA")
r1.save()