Skip to content

Make DjangoDebugContext wait for nested fields #591

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

Merged
merged 4 commits into from
Aug 7, 2019
Merged
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
6 changes: 5 additions & 1 deletion graphene_django/debug/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ def __init__(self):
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):
Expand Down
67 changes: 67 additions & 0 deletions graphene_django/debug/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,73 @@ def resolve_reporter(self, info, **args):
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()
Expand Down