2016-09-21 08:30:36 +03:00
|
|
|
Django Debug Middleware
|
|
|
|
=======================
|
|
|
|
|
|
|
|
You can debug your GraphQL queries in a similar way to
|
|
|
|
`django-debug-toolbar <https://django-debug-toolbar.readthedocs.org/>`__,
|
2020-12-31 02:37:57 +03:00
|
|
|
but outputting in the results in GraphQL response as fields, instead of
|
2021-03-02 21:45:46 +03:00
|
|
|
the graphical HTML interface. Exceptions with their stack traces are also exposed.
|
2016-09-21 08:30:36 +03:00
|
|
|
|
|
|
|
For that, you will need to add the plugin in your graphene schema.
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
|
|
|
For use the Django Debug plugin in Graphene:
|
|
|
|
|
|
|
|
* Add ``graphene_django.debug.DjangoDebugMiddleware`` into ``MIDDLEWARE`` in the ``GRAPHENE`` settings.
|
|
|
|
|
2019-05-20 12:48:28 +03:00
|
|
|
* Add the ``debug`` field into the schema root ``Query`` with the value ``graphene.Field(DjangoDebug, name='_debug')``.
|
2016-09-21 08:30:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
from graphene_django.debug import DjangoDebug
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
# ...
|
2019-05-20 12:48:28 +03:00
|
|
|
debug = graphene.Field(DjangoDebug, name='_debug')
|
2016-09-21 08:30:36 +03:00
|
|
|
|
|
|
|
schema = graphene.Schema(query=Query)
|
|
|
|
|
|
|
|
|
|
|
|
And in your ``settings.py``:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
GRAPHENE = {
|
2019-04-26 18:48:37 +03:00
|
|
|
...
|
2016-09-21 08:30:36 +03:00
|
|
|
'MIDDLEWARE': [
|
|
|
|
'graphene_django.debug.DjangoDebugMiddleware',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
Querying
|
|
|
|
--------
|
|
|
|
|
2020-12-31 02:37:57 +03:00
|
|
|
You can query it for outputting all the sql transactions that happened in
|
2016-09-21 08:30:36 +03:00
|
|
|
the GraphQL request, like:
|
|
|
|
|
2016-09-26 11:13:31 +03:00
|
|
|
.. code::
|
2016-09-21 08:30:36 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
# A example that will use the ORM for interact with the DB
|
|
|
|
allIngredients {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id,
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Here is the debug field that will output the SQL queries
|
2019-05-20 12:48:28 +03:00
|
|
|
_debug {
|
2016-09-21 08:30:36 +03:00
|
|
|
sql {
|
|
|
|
rawSql
|
|
|
|
}
|
2021-03-02 21:45:46 +03:00
|
|
|
exceptions {
|
|
|
|
message
|
|
|
|
stack
|
|
|
|
}
|
2016-09-21 08:30:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 12:48:28 +03:00
|
|
|
Note that the ``_debug`` field must be the last field in your query.
|