Merge branch 'main' into fix-m2m-filter-type

This commit is contained in:
Firas K 2023-03-01 11:22:56 +03:00 committed by GitHub
commit ee6f1a6821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 19 deletions

View File

@ -9,32 +9,32 @@ from .types import DjangoDebug
class DjangoDebugContext: class DjangoDebugContext:
def __init__(self): def __init__(self):
self.debug_promise = None self.debug_result = None
self.promises = [] self.results = []
self.object = DjangoDebug(sql=[], exceptions=[]) self.object = DjangoDebug(sql=[], exceptions=[])
self.enable_instrumentation() self.enable_instrumentation()
def get_debug_promise(self): def get_debug_result(self):
if not self.debug_promise: if not self.debug_result:
self.debug_promise = Promise.all(self.promises) self.debug_result = self.results
self.promises = [] self.results = []
return self.debug_promise.then(self.on_resolve_all_promises).get() return self.on_resolve_all_results()
def on_resolve_error(self, value): def on_resolve_error(self, value):
if hasattr(self, "object"): if hasattr(self, "object"):
self.object.exceptions.append(wrap_exception(value)) self.object.exceptions.append(wrap_exception(value))
return Promise.reject(value) return value
def on_resolve_all_promises(self, values): def on_resolve_all_results(self):
if self.promises: if self.results:
self.debug_promise = None self.debug_result = None
return self.get_debug_promise() return self.get_debug_result()
self.disable_instrumentation() self.disable_instrumentation()
return self.object return self.object
def add_promise(self, promise): def add_result(self, result):
if self.debug_promise: if self.debug_result:
self.promises.append(promise) self.results.append(result)
def enable_instrumentation(self): def enable_instrumentation(self):
# This is thread-safe because database connections are thread-local. # This is thread-safe because database connections are thread-local.
@ -62,10 +62,10 @@ class DjangoDebugMiddleware:
) )
) )
if info.schema.get_type("DjangoDebug") == info.return_type: if info.schema.get_type("DjangoDebug") == info.return_type:
return context.django_debug.get_debug_promise() return context.django_debug.get_debug_result()
try: try:
promise = next(root, info, **args) result = next(root, info, **args)
except Exception as e: except Exception as e:
return context.django_debug.on_resolve_error(e) return context.django_debug.on_resolve_error(e)
context.django_debug.add_promise(promise) context.django_debug.add_result(result)
return promise return result

View File

@ -23,6 +23,7 @@ passenv = *
usedevelop = True usedevelop = True
setenv = setenv =
DJANGO_SETTINGS_MODULE=examples.django_test_settings DJANGO_SETTINGS_MODULE=examples.django_test_settings
PYTHONPATH=.
deps = deps =
-e.[test] -e.[test]
psycopg2-binary psycopg2-binary