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

Remove promise based debug middleware #1388

Merged
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
38 changes: 19 additions & 19 deletions graphene_django/debug/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@

class DjangoDebugContext:
def __init__(self):
self.debug_promise = None
self.promises = []
self.debug_result = None
self.results = []
self.object = DjangoDebug(sql=[], exceptions=[])
self.enable_instrumentation()

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).get()
def get_debug_result(self):
if not self.debug_result:
self.debug_result = self.results
self.results = []
return self.on_resolve_all_results()

def on_resolve_error(self, value):
if hasattr(self, "object"):
self.object.exceptions.append(wrap_exception(value))
return Promise.reject(value)
return value

def on_resolve_all_promises(self, values):
if self.promises:
self.debug_promise = None
return self.get_debug_promise()
def on_resolve_all_results(self):
if self.results:
self.debug_result = None
return self.get_debug_result()
self.disable_instrumentation()
return self.object

def add_promise(self, promise):
if self.debug_promise:
self.promises.append(promise)
def add_result(self, result):
if self.debug_result:
self.results.append(result)

def enable_instrumentation(self):
# This is thread-safe because database connections are thread-local.
Expand Down Expand Up @@ -62,10 +62,10 @@ def resolve(self, next, root, info, **args):
)
)
if info.schema.get_type("DjangoDebug") == info.return_type:
return context.django_debug.get_debug_promise()
return context.django_debug.get_debug_result()
try:
promise = next(root, info, **args)
result = next(root, info, **args)
except Exception as e:
return context.django_debug.on_resolve_error(e)
context.django_debug.add_promise(promise)
return promise
context.django_debug.add_result(result)
return result
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ passenv = *
usedevelop = True
setenv =
DJANGO_SETTINGS_MODULE=examples.django_test_settings
PYTHONPATH=.
deps =
-e.[test]
psycopg2-binary
Expand Down