12.Django Debug Middleware-Django调试中间件
You can debug your GraphQL queries in a similar way to django-debug-toolbar, but outputing in the results in GraphQL response as fields, instead of the graphical HTML interface.
您可以以类似于djdjg -debug-toolbar的方式调试GraphQL查询,但是将结果输出为GraphQL响应字段,而不是图形化的HTML界面。
For that, you will need to add the plugin in your graphene schema.
为此,你需要将插件添加到你的graphene 模式中。
Installation 安装
For use the Django Debug plugin in Graphene:
在Graphene中使用Django调试插件:
- Add
graphene_django.debug.DjangoDebugMiddleware
intoMIDDLEWARE
in theGRAPHENE
settings. - 在
GRAPHENE
设置的MIDDLEWARE
中添加graphene_django.debug.DjangoDebugMiddleware
。 - Add the
debug
field into the schema rootQuery
with the valuegraphene.Field(DjangoDebug, name='_debug')
. - 将
debug
字段添加到模式根目录Query
中,值为graphene.Field(DjangoDebug, name='_debug')
。
from graphene_django.debug import DjangoDebug
class Query(graphene.ObjectType):
# ...
debug = graphene.Field(DjangoDebug, name='_debug')
schema = graphene.Schema(query=Query)
And in your settings.py
:
在你的settings.py
中:
GRAPHENE = {
...
'MIDDLEWARE': [
'graphene_django.debug.DjangoDebugMiddleware',
]
}
Querying 查询
You can query it for outputing all the sql transactions that happened in the GraphQL request, like:
您可以查询它输出所有发生在GraphQL请求中的sql事务,如:
{
# A example that will use the ORM for interact with the DB
# 一个使用ORM与数据库交互的例子
allIngredients {
edges {
node {
id,
name
}
}
}
# Here is the debug field that will output the SQL queries
# 下面是将输出SQL查询的调试字段
_debug {
sql {
rawSql
}
}
}
Note that the _debug
field must be the last field in your query.
注意,_debug
字段必须是查询中的最后一个字段。