From 11605dcdc6806cbd9d466e84a5e50e0958a438f0 Mon Sep 17 00:00:00 2001 From: Tomasz Kontusz Date: Wed, 7 Aug 2019 09:09:17 +0200 Subject: [PATCH] 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 --- graphene_django/debug/middleware.py | 6 +- graphene_django/debug/tests/test_query.py | 67 +++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/graphene_django/debug/middleware.py b/graphene_django/debug/middleware.py index 48d471f..0fe3fe3 100644 --- a/graphene_django/debug/middleware.py +++ b/graphene_django/debug/middleware.py @@ -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): diff --git a/graphene_django/debug/tests/test_query.py b/graphene_django/debug/tests/test_query.py index af69715..db8f275 100644 --- a/graphene_django/debug/tests/test_query.py +++ b/graphene_django/debug/tests/test_query.py @@ -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()