Skip to content

Commit a153a01

Browse files
committed
Improved debug using plugin structure
1 parent 2ad5bc2 commit a153a01

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .schema import DebugSchema
1+
from .plugin import DjangoDebugPlugin
22
from .types import DjangoDebug
33

4-
__all__ = ['DebugSchema', 'DjangoDebug']
4+
__all__ = ['DjangoDebugPlugin', 'DjangoDebug']

graphene/contrib/django/debug/schema.py renamed to graphene/contrib/django/debug/plugin.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from contextlib import contextmanager
12
from django.db import connections
23

4+
from ....plugins import Plugin
35
from ....core.schema import Schema
46
from ....core.types import Field
57
from .sql.tracking import unwrap_cursor, wrap_cursor
@@ -41,15 +43,11 @@ def debug_objecttype(objecttype):
4143
{'debug': Field(DjangoDebug, name='__debug')})
4244

4345

44-
class DebugSchema(Schema):
45-
46-
@property
47-
def query(self):
48-
return self._query
49-
50-
@query.setter
51-
def query(self, value):
52-
self._query = value and debug_objecttype(value)
46+
class DjangoDebugPlugin(Plugin):
47+
def transform_type(self, _type):
48+
if _type == self.schema.query:
49+
return debug_objecttype(_type)
50+
return _type
5351

5452
def enable_instrumentation(self, wrapped_root):
5553
# This is thread-safe because database connections are thread-local.
@@ -60,9 +58,9 @@ def disable_instrumentation(self):
6058
for connection in connections.all():
6159
unwrap_cursor(connection)
6260

63-
def execute(self, query, root=None, *args, **kwargs):
64-
wrapped_root = WrappedRoot(root=root)
65-
self.enable_instrumentation(wrapped_root)
66-
result = super(DebugSchema, self).execute(query, wrapped_root, *args, **kwargs)
61+
@contextmanager
62+
def context_execution(self, executor):
63+
executor['root'] = WrappedRoot(root=executor['root'])
64+
self.enable_instrumentation(executor['root'])
65+
yield executor
6766
self.disable_instrumentation()
68-
return result

graphene/contrib/django/debug/tests/test_query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from graphene.contrib.django import DjangoObjectType
55

66
from ...tests.models import Reporter
7-
from ..schema import DebugSchema
7+
from ..plugin import DjangoDebugPlugin
88

99
# from examples.starwars_django.models import Character
1010

@@ -24,7 +24,7 @@ class Meta:
2424

2525
class Query(graphene.ObjectType):
2626
reporter = graphene.Field(ReporterType)
27-
all_reporters = ReporterType.List
27+
all_reporters = ReporterType.List()
2828

2929
def resolve_all_reporters(self, *args, **kwargs):
3030
return Reporter.objects.all()
@@ -64,7 +64,7 @@ def resolve_reporter(self, *args, **kwargs):
6464
}]
6565
}
6666
}
67-
schema = DebugSchema(query=Query)
67+
schema = graphene.Schema(query=Query, plugins=[DjangoDebugPlugin()])
6868
result = schema.execute(query)
6969
assert not result.errors
7070
assert result.data == expected

graphene/contrib/django/debug/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55

66
class DjangoDebug(ObjectType):
7-
sql = Field(DjangoDebugSQL.List)
7+
sql = Field(DjangoDebugSQL.List())

graphene/core/schema.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,25 @@ def get_type(self, type_name):
127127
def types(self):
128128
return self._types_names
129129

130-
def execute(self, request='', root=None, vars=None,
131-
operation_name=None, **kwargs):
132-
root = root or object()
133-
return self.executor.execute(
130+
def execute(self, request='', root=None, args=None, **kwargs):
131+
executor = kwargs
132+
executor['root'] = root
133+
executor['args'] = args
134+
contexts = []
135+
for plugin in self.plugins:
136+
if not hasattr(plugin, 'context_execution'):
137+
continue
138+
context = plugin.context_execution(executor)
139+
executor = context.__enter__()
140+
contexts.append((context, executor))
141+
result = self.executor.execute(
134142
self.schema,
135143
request,
136-
root=root,
137-
args=vars,
138-
operation_name=operation_name,
139-
**kwargs
144+
**executor
140145
)
146+
for context, value in contexts[::-1]:
147+
context.__exit__(None, None, None)
148+
return result
141149

142150
def introspect(self):
143151
return self.execute(introspection_query).data

0 commit comments

Comments
 (0)