Skip to content

Commit 930f084

Browse files
committed
Fixed DjangoDebugPlugin. Improved Django views
1 parent a161738 commit 930f084

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

graphene/contrib/django/debug/plugin.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.db import connections
44

55
from ....core.types import Field
6+
from ....core.schema import GraphQLSchema
67
from ....plugins import Plugin
78
from .sql.tracking import unwrap_cursor, wrap_cursor
89
from .sql.types import DjangoDebugSQL
@@ -45,11 +46,6 @@ def debug_objecttype(objecttype):
4546

4647
class DjangoDebugPlugin(Plugin):
4748

48-
def transform_type(self, _type):
49-
if _type == self.schema.query:
50-
return
51-
return _type
52-
5349
def enable_instrumentation(self, wrapped_root):
5450
# This is thread-safe because database connections are thread-local.
5551
for connection in connections.all():
@@ -62,10 +58,16 @@ def disable_instrumentation(self):
6258
def wrap_schema(self, schema_type):
6359
query = schema_type._query
6460
if query:
65-
class_type = self.schema.objecttype(schema_type._query)
61+
class_type = self.schema.objecttype(schema_type.get_query_type())
6662
assert class_type, 'The query in schema is not constructed with graphene'
6763
_type = debug_objecttype(class_type)
68-
schema_type._query = self.schema.T(_type)
64+
self.schema.register(_type, force=True)
65+
return GraphQLSchema(
66+
self.schema,
67+
self.schema.T(_type),
68+
schema_type.get_mutation_type(),
69+
schema_type.get_subscription_type()
70+
)
6971
return schema_type
7072

7173
@contextmanager

graphene/contrib/django/debug/sql/types.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .....core import Float, ObjectType, String
1+
from .....core import Float, ObjectType, String, Boolean
22

33

44
class DjangoDebugSQL(ObjectType):
@@ -10,8 +10,8 @@ class DjangoDebugSQL(ObjectType):
1010
params = String()
1111
start_time = Float()
1212
stop_time = Float()
13-
is_slow = String()
14-
is_select = String()
13+
is_slow = Boolean()
14+
is_select = Boolean()
1515

1616
trans_id = String()
1717
trans_status = String()

graphene/contrib/django/tests/test_urls.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ def resolve_raises(self, *args):
2929
def get_node(self, id):
3030
pass
3131

32-
schema = Schema(query=Human)
32+
33+
class Query(graphene.ObjectType):
34+
human = graphene.Field(Human)
35+
36+
def resolve_human(self, args, info):
37+
return Human()
38+
39+
40+
schema = Schema(query=Query)
3341

3442

3543
urlpatterns = [

graphene/contrib/django/tests/test_views.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,36 @@ def format_response(response):
77

88
def test_client_get_good_query(settings, client):
99
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
10-
response = client.get('/graphql', {'query': '{ headline }'})
10+
response = client.get('/graphql', {'query': '{ human { headline } }'})
1111
json_response = format_response(response)
1212
expected_json = {
1313
'data': {
14-
'headline': None
14+
'human': {
15+
'headline': None
16+
}
1517
}
1618
}
1719
assert json_response == expected_json
1820

1921

2022
def test_client_get_good_query_with_raise(settings, client):
2123
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
22-
response = client.get('/graphql', {'query': '{ raises }'})
24+
response = client.get('/graphql', {'query': '{ human { raises } }'})
2325
json_response = format_response(response)
2426
assert json_response['errors'][0]['message'] == 'This field should raise exception'
25-
assert json_response['data']['raises'] is None
27+
assert json_response['data']['human']['raises'] is None
2628

2729

2830
def test_client_post_good_query_json(settings, client):
2931
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
3032
response = client.post(
31-
'/graphql', json.dumps({'query': '{ headline }'}), 'application/json')
33+
'/graphql', json.dumps({'query': '{ human { headline } }'}), 'application/json')
3234
json_response = format_response(response)
3335
expected_json = {
3436
'data': {
35-
'headline': None
37+
'human': {
38+
'headline': None
39+
}
3640
}
3741
}
3842
assert json_response == expected_json
@@ -41,11 +45,13 @@ def test_client_post_good_query_json(settings, client):
4145
def test_client_post_good_query_graphql(settings, client):
4246
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
4347
response = client.post(
44-
'/graphql', '{ headline }', 'application/graphql')
48+
'/graphql', '{ human { headline } }', 'application/graphql')
4549
json_response = format_response(response)
4650
expected_json = {
4751
'data': {
48-
'headline': None
52+
'human': {
53+
'headline': None
54+
}
4955
}
5056
}
5157
assert json_response == expected_json

graphene/contrib/django/views.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ def __init__(self, schema, **kwargs):
1212
**kwargs
1313
)
1414

15-
def get_root_value(self, request):
16-
return self.graphene_schema.query(super(GraphQLView, self).get_root_value(request))
15+
def execute(self, *args, **kwargs):
16+
return self.graphene_schema.execute(*args, **kwargs)

graphene/core/schema.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def schema(self):
8484
mutation=self.T(self.mutation),
8585
subscription=self.T(self.subscription))
8686

87-
def register(self, object_type):
87+
def register(self, object_type, force=False):
8888
type_name = object_type._meta.type_name
89-
registered_object_type = self._types_names.get(type_name, None)
89+
registered_object_type = not force and self._types_names.get(type_name, None)
9090
if registered_object_type:
9191
assert registered_object_type == object_type, 'Type {} already registered with other object type'.format(
9292
type_name)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def run_tests(self):
6767
'django': [
6868
'Django>=1.6.0,<1.9',
6969
'singledispatch>=3.4.0.3',
70-
'graphql-django-view>=1.0.0',
70+
'graphql-django-view>=1.1.0',
7171
],
7272
},
7373

0 commit comments

Comments
 (0)