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 into MIDDLEWARE in the GRAPHENE settings.
  • GRAPHENE设置的MIDDLEWARE中添加graphene_django.debug.DjangoDebugMiddleware
  • Add the debug field into the schema root Query with the value graphene.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字段必须是查询中的最后一个字段。

posted @ 2020-09-25 07:21  双葫  阅读(132)  评论(0编辑  收藏  举报