Fixed django debug plugin

This commit is contained in:
Syrus Akbary 2015-12-10 22:02:00 -08:00
parent 345fb40a5e
commit 65a0a46d45
5 changed files with 18 additions and 55 deletions

View File

@ -3,6 +3,7 @@ from contextlib import contextmanager
from django.db import connections from django.db import connections
from ....core.types import Field from ....core.types import Field
from ....core.schema import GraphQLSchema
from ....plugins import Plugin from ....plugins import Plugin
from .sql.tracking import unwrap_cursor, wrap_cursor from .sql.tracking import unwrap_cursor, wrap_cursor
from .sql.types import DjangoDebugSQL from .sql.types import DjangoDebugSQL
@ -45,11 +46,6 @@ def debug_objecttype(objecttype):
class DjangoDebugPlugin(Plugin): class DjangoDebugPlugin(Plugin):
def transform_type(self, _type):
if _type == self.schema.query:
return
return _type
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.
for connection in connections.all(): for connection in connections.all():
@ -62,10 +58,16 @@ class DjangoDebugPlugin(Plugin):
def wrap_schema(self, schema_type): def wrap_schema(self, schema_type):
query = schema_type._query query = schema_type._query
if query: if query:
class_type = self.schema.objecttype(schema_type._query) class_type = self.schema.objecttype(schema_type.get_query_type())
assert class_type, 'The query in schema is not constructed with graphene' assert class_type, 'The query in schema is not constructed with graphene'
_type = debug_objecttype(class_type) _type = debug_objecttype(class_type)
schema_type._query = self.schema.T(_type) self.schema.register(_type, force=True)
return GraphQLSchema(
self.schema,
self.schema.T(_type),
schema_type.get_mutation_type(),
schema_type.get_subscription_type()
)
return schema_type return schema_type
@contextmanager @contextmanager

View File

@ -1,4 +1,4 @@
from .....core import Float, ObjectType, String from .....core import Float, ObjectType, String, Boolean
class DjangoDebugSQL(ObjectType): class DjangoDebugSQL(ObjectType):
@ -10,8 +10,8 @@ class DjangoDebugSQL(ObjectType):
params = String() params = String()
start_time = Float() start_time = Float()
stop_time = Float() stop_time = Float()
is_slow = String() is_slow = Boolean()
is_select = String() is_select = Boolean()
trans_id = String() trans_id = String()
trans_status = String() trans_status = String()

View File

@ -1,10 +1,4 @@
from django.http import HttpResponseNotAllowed from graphql_django_view import GraphQLView as BaseGraphQLView
from django.http.response import HttpResponseBadRequest
from graphql.core import Source, parse, validate
from graphql.core.execution import ExecutionResult
from graphql.core.utils.get_operation_ast import get_operation_ast
from graphql_django_view import GraphQLView as BaseGraphQLView, HttpError
class GraphQLView(BaseGraphQLView): class GraphQLView(BaseGraphQLView):
@ -18,38 +12,5 @@ class GraphQLView(BaseGraphQLView):
**kwargs **kwargs
) )
def execute_graphql_request(self, request): def execute(self, *args, **kwargs):
query, variables, operation_name = self.get_graphql_params(request, self.parse_body(request)) return self.graphene_schema.execute(*args, **kwargs)
if not query:
raise HttpError(HttpResponseBadRequest('Must provide query string.'))
source = Source(query, name='GraphQL request')
try:
document_ast = parse(source)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)
validation_errors = validate(self.schema, document_ast)
if validation_errors:
return ExecutionResult(invalid=True, errors=validation_errors)
if request.method.lower() == 'get':
operation_ast = get_operation_ast(document_ast, operation_name)
if operation_ast and operation_ast.operation != 'query':
raise HttpError(HttpResponseNotAllowed(
['POST'], 'Can only perform a {} operation from a POST request.'.format(operation_ast.operation)
))
try:
return self.graphene_schema.execute(
document_ast,
self.get_root_value(request),
variables,
operation_name=operation_name,
validate_ast=False,
request_context=request
)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)

View File

@ -84,9 +84,9 @@ class Schema(object):
mutation=self.T(self.mutation), mutation=self.T(self.mutation),
subscription=self.T(self.subscription)) subscription=self.T(self.subscription))
def register(self, object_type): def register(self, object_type, force=False):
type_name = object_type._meta.type_name type_name = object_type._meta.type_name
registered_object_type = self._types_names.get(type_name, None) registered_object_type = not force and self._types_names.get(type_name, None)
if registered_object_type: if registered_object_type:
assert registered_object_type == object_type, 'Type {} already registered with other object type'.format( assert registered_object_type == object_type, 'Type {} already registered with other object type'.format(
type_name) type_name)

View File

@ -67,7 +67,7 @@ setup(
'django': [ 'django': [
'Django>=1.6.0,<1.9', 'Django>=1.6.0,<1.9',
'singledispatch>=3.4.0.3', 'singledispatch>=3.4.0.3',
'graphql-django-view>=1.0.0', 'graphql-django-view>=1.1.0',
], ],
}, },