mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Fixed DjangoDebugPlugin. Improved Django views
This commit is contained in:
parent
a161738f3d
commit
930f084912
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 = [
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user