diff --git a/graphene/contrib/django/debug/plugin.py b/graphene/contrib/django/debug/plugin.py index 4b6b1b5d..86f8da58 100644 --- a/graphene/contrib/django/debug/plugin.py +++ b/graphene/contrib/django/debug/plugin.py @@ -3,6 +3,7 @@ from contextlib import contextmanager from django.db import connections from ....core.types import Field +from ....core.schema import GraphQLSchema from ....plugins import Plugin from .sql.tracking import unwrap_cursor, wrap_cursor from .sql.types import DjangoDebugSQL @@ -45,11 +46,6 @@ def debug_objecttype(objecttype): class DjangoDebugPlugin(Plugin): - def transform_type(self, _type): - if _type == self.schema.query: - return - return _type - def enable_instrumentation(self, wrapped_root): # This is thread-safe because database connections are thread-local. for connection in connections.all(): @@ -62,10 +58,16 @@ class DjangoDebugPlugin(Plugin): def wrap_schema(self, schema_type): query = schema_type._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' _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 @contextmanager diff --git a/graphene/contrib/django/debug/sql/types.py b/graphene/contrib/django/debug/sql/types.py index 7ee1894f..5df5e9d8 100644 --- a/graphene/contrib/django/debug/sql/types.py +++ b/graphene/contrib/django/debug/sql/types.py @@ -1,4 +1,4 @@ -from .....core import Float, ObjectType, String +from .....core import Float, ObjectType, String, Boolean class DjangoDebugSQL(ObjectType): @@ -10,8 +10,8 @@ class DjangoDebugSQL(ObjectType): params = String() start_time = Float() stop_time = Float() - is_slow = String() - is_select = String() + is_slow = Boolean() + is_select = Boolean() trans_id = String() trans_status = String() diff --git a/graphene/contrib/django/tests/test_urls.py b/graphene/contrib/django/tests/test_urls.py index 409471d4..9d38980e 100644 --- a/graphene/contrib/django/tests/test_urls.py +++ b/graphene/contrib/django/tests/test_urls.py @@ -29,7 +29,15 @@ class Human(DjangoNode): def get_node(self, id): 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 = [ diff --git a/graphene/contrib/django/tests/test_views.py b/graphene/contrib/django/tests/test_views.py index f82be99f..b4e1b367 100644 --- a/graphene/contrib/django/tests/test_views.py +++ b/graphene/contrib/django/tests/test_views.py @@ -7,11 +7,13 @@ def format_response(response): def test_client_get_good_query(settings, client): 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) expected_json = { 'data': { - 'headline': None + 'human': { + 'headline': None + } } } assert json_response == expected_json @@ -19,20 +21,22 @@ def test_client_get_good_query(settings, client): def test_client_get_good_query_with_raise(settings, client): 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) 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): settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls' response = client.post( - '/graphql', json.dumps({'query': '{ headline }'}), 'application/json') + '/graphql', json.dumps({'query': '{ human { headline } }'}), 'application/json') json_response = format_response(response) expected_json = { 'data': { - 'headline': None + 'human': { + 'headline': None + } } } assert json_response == expected_json @@ -41,11 +45,13 @@ def test_client_post_good_query_json(settings, client): def test_client_post_good_query_graphql(settings, client): settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls' response = client.post( - '/graphql', '{ headline }', 'application/graphql') + '/graphql', '{ human { headline } }', 'application/graphql') json_response = format_response(response) expected_json = { 'data': { - 'headline': None + 'human': { + 'headline': None + } } } assert json_response == expected_json diff --git a/graphene/contrib/django/views.py b/graphene/contrib/django/views.py index ad245d72..9a4bd96e 100644 --- a/graphene/contrib/django/views.py +++ b/graphene/contrib/django/views.py @@ -12,5 +12,5 @@ class GraphQLView(BaseGraphQLView): **kwargs ) - def get_root_value(self, request): - return self.graphene_schema.query(super(GraphQLView, self).get_root_value(request)) + def execute(self, *args, **kwargs): + return self.graphene_schema.execute(*args, **kwargs) diff --git a/graphene/core/schema.py b/graphene/core/schema.py index f8f26dc1..c8695317 100644 --- a/graphene/core/schema.py +++ b/graphene/core/schema.py @@ -84,9 +84,9 @@ class Schema(object): mutation=self.T(self.mutation), subscription=self.T(self.subscription)) - def register(self, object_type): + def register(self, object_type, force=False): 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: assert registered_object_type == object_type, 'Type {} already registered with other object type'.format( type_name) diff --git a/setup.py b/setup.py index 2c36fcec..7ef809ed 100644 --- a/setup.py +++ b/setup.py @@ -67,7 +67,7 @@ setup( 'django': [ 'Django>=1.6.0,<1.9', 'singledispatch>=3.4.0.3', - 'graphql-django-view>=1.0.0', + 'graphql-django-view>=1.1.0', ], },