Fixed DjangoDebugPlugin. Improved Django views

This commit is contained in:
Syrus Akbary 2015-12-10 22:11:43 -08:00
parent a161738f3d
commit 930f084912
7 changed files with 40 additions and 24 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

@ -29,7 +29,15 @@ class Human(DjangoNode):
def get_node(self, id): def get_node(self, id):
pass pass
schema = Schema(query=Human)
class Query(graphene.ObjectType):
human = graphene.Field(Human)
def resolve_human(self, args, info):
return Human()
schema = Schema(query=Query)
urlpatterns = [ urlpatterns = [

View File

@ -7,45 +7,51 @@ def format_response(response):
def test_client_get_good_query(settings, client): def test_client_get_good_query(settings, client):
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls' settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
response = client.get('/graphql', {'query': '{ headline }'}) response = client.get('/graphql', {'query': '{ human { headline } }'})
json_response = format_response(response) json_response = format_response(response)
expected_json = { expected_json = {
'data': { 'data': {
'human': {
'headline': None 'headline': None
} }
} }
}
assert json_response == expected_json assert json_response == expected_json
def test_client_get_good_query_with_raise(settings, client): def test_client_get_good_query_with_raise(settings, client):
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls' settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
response = client.get('/graphql', {'query': '{ raises }'}) response = client.get('/graphql', {'query': '{ human { raises } }'})
json_response = format_response(response) json_response = format_response(response)
assert json_response['errors'][0]['message'] == 'This field should raise exception' assert json_response['errors'][0]['message'] == 'This field should raise exception'
assert json_response['data']['raises'] is None assert json_response['data']['human']['raises'] is None
def test_client_post_good_query_json(settings, client): def test_client_post_good_query_json(settings, client):
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls' settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
response = client.post( response = client.post(
'/graphql', json.dumps({'query': '{ headline }'}), 'application/json') '/graphql', json.dumps({'query': '{ human { headline } }'}), 'application/json')
json_response = format_response(response) json_response = format_response(response)
expected_json = { expected_json = {
'data': { 'data': {
'human': {
'headline': None 'headline': None
} }
} }
}
assert json_response == expected_json assert json_response == expected_json
def test_client_post_good_query_graphql(settings, client): def test_client_post_good_query_graphql(settings, client):
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls' settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
response = client.post( response = client.post(
'/graphql', '{ headline }', 'application/graphql') '/graphql', '{ human { headline } }', 'application/graphql')
json_response = format_response(response) json_response = format_response(response)
expected_json = { expected_json = {
'data': { 'data': {
'human': {
'headline': None 'headline': None
} }
} }
}
assert json_response == expected_json assert json_response == expected_json

View File

@ -12,5 +12,5 @@ class GraphQLView(BaseGraphQLView):
**kwargs **kwargs
) )
def get_root_value(self, request): def execute(self, *args, **kwargs):
return self.graphene_schema.query(super(GraphQLView, self).get_root_value(request)) return self.graphene_schema.execute(*args, **kwargs)

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',
], ],
}, },