Improved debug using plugin structure

This commit is contained in:
Syrus Akbary 2015-12-06 03:59:44 -08:00
parent 2ad5bc203a
commit a153a01f6b
5 changed files with 34 additions and 28 deletions

View File

@ -1,4 +1,4 @@
from .schema import DebugSchema from .plugin import DjangoDebugPlugin
from .types import DjangoDebug from .types import DjangoDebug
__all__ = ['DebugSchema', 'DjangoDebug'] __all__ = ['DjangoDebugPlugin', 'DjangoDebug']

View File

@ -1,5 +1,7 @@
from contextlib import contextmanager
from django.db import connections from django.db import connections
from ....plugins import Plugin
from ....core.schema import Schema from ....core.schema import Schema
from ....core.types import Field from ....core.types import Field
from .sql.tracking import unwrap_cursor, wrap_cursor from .sql.tracking import unwrap_cursor, wrap_cursor
@ -41,15 +43,11 @@ def debug_objecttype(objecttype):
{'debug': Field(DjangoDebug, name='__debug')}) {'debug': Field(DjangoDebug, name='__debug')})
class DebugSchema(Schema): class DjangoDebugPlugin(Plugin):
def transform_type(self, _type):
@property if _type == self.schema.query:
def query(self): return debug_objecttype(_type)
return self._query return _type
@query.setter
def query(self, value):
self._query = value and debug_objecttype(value)
def enable_instrumentation(self, wrapped_root): def enable_instrumentation(self, wrapped_root):
# This is thread-safe because database connections are thread-local. # This is thread-safe because database connections are thread-local.
@ -60,9 +58,9 @@ class DebugSchema(Schema):
for connection in connections.all(): for connection in connections.all():
unwrap_cursor(connection) unwrap_cursor(connection)
def execute(self, query, root=None, *args, **kwargs): @contextmanager
wrapped_root = WrappedRoot(root=root) def context_execution(self, executor):
self.enable_instrumentation(wrapped_root) executor['root'] = WrappedRoot(root=executor['root'])
result = super(DebugSchema, self).execute(query, wrapped_root, *args, **kwargs) self.enable_instrumentation(executor['root'])
yield executor
self.disable_instrumentation() self.disable_instrumentation()
return result

View File

@ -4,7 +4,7 @@ import graphene
from graphene.contrib.django import DjangoObjectType from graphene.contrib.django import DjangoObjectType
from ...tests.models import Reporter from ...tests.models import Reporter
from ..schema import DebugSchema from ..plugin import DjangoDebugPlugin
# from examples.starwars_django.models import Character # from examples.starwars_django.models import Character
@ -24,7 +24,7 @@ def test_should_query_well():
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
reporter = graphene.Field(ReporterType) reporter = graphene.Field(ReporterType)
all_reporters = ReporterType.List all_reporters = ReporterType.List()
def resolve_all_reporters(self, *args, **kwargs): def resolve_all_reporters(self, *args, **kwargs):
return Reporter.objects.all() return Reporter.objects.all()
@ -64,7 +64,7 @@ def test_should_query_well():
}] }]
} }
} }
schema = DebugSchema(query=Query) schema = graphene.Schema(query=Query, plugins=[DjangoDebugPlugin()])
result = schema.execute(query) result = schema.execute(query)
assert not result.errors assert not result.errors
assert result.data == expected assert result.data == expected

View File

@ -4,4 +4,4 @@ from .sql.types import DjangoDebugSQL
class DjangoDebug(ObjectType): class DjangoDebug(ObjectType):
sql = Field(DjangoDebugSQL.List) sql = Field(DjangoDebugSQL.List())

View File

@ -127,17 +127,25 @@ class Schema(object):
def types(self): def types(self):
return self._types_names return self._types_names
def execute(self, request='', root=None, vars=None, def execute(self, request='', root=None, args=None, **kwargs):
operation_name=None, **kwargs): executor = kwargs
root = root or object() executor['root'] = root
return self.executor.execute( executor['args'] = args
contexts = []
for plugin in self.plugins:
if not hasattr(plugin, 'context_execution'):
continue
context = plugin.context_execution(executor)
executor = context.__enter__()
contexts.append((context, executor))
result = self.executor.execute(
self.schema, self.schema,
request, request,
root=root, **executor
args=vars,
operation_name=operation_name,
**kwargs
) )
for context, value in contexts[::-1]:
context.__exit__(None, None, None)
return result
def introspect(self): def introspect(self):
return self.execute(introspection_query).data return self.execute(introspection_query).data