uroshcs/graphql-laravel-query-debug

laraver rebing/graphql-laravel 包的查询调试器

v1.0 2018-05-29 09:06 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:21:10 UTC


README

这是一个简单的包,它创建了一个 graphql 查询类型,允许获取执行过的数据库查询列表。这可以用来检查哪些数据库查询被执行了,以及执行了多少。

此包需要安装并发布 (php artisan vendor:publish ...) 的 Rebing's graphql-laravel 包。如果您尚未安装和发布此包,请先进行此操作。这样您将拥有需要编辑的 app/config/graphql.php 文件。

安装

依赖项

安装

1- 在您的 composer.json 中通过 Composer 引入此包。

{
  "require": {
    "uroshcs/graphql-laravel-query-debug": "dev-master"
  }
}

2- 运行 Composer 以安装或更新新需求。

$ composer install

或者

$ composer update

3- 将服务提供者添加到您的 app/config/app.php 文件中

Uroshcs\GraphQLQueryDebugger\DebuggerServiceProvider::class,

4-queries_executed 查询添加到您的 app/config/graphql.php 文件中

'schemas' => [
    'default' => [
        'query' => [
            // other query types
            'queries_executed' => Uroshcs\GraphQLQueryDebugger\GraphQL\QueriesExecutedQuery::class,
        ],
        // mutations, middleware
    ],
],

5-query_executed 类型添加到您的 app/config/graphql.php 文件中

'types' => [
    // other types
    'query_executed' => Uroshcs\GraphQLQueryDebugger\GraphQL\QueryExecutedType::class,
],

使用方法

完整的查询包含所有可能的字段如下所示

{
    # other fields

    queries_executed {
        sql
        bindings
        time
        connection_name
    }
}

为了使此包正确执行其工作,需要确保 queries_executed 是最后一个查询,以便从其他类型积累数据库查询。

例如,一个查询如下所示

{
    users (limit: 10, page: 1) {
        total
        data {
            id
            username
            email
            posts_count
        }
    }

    queries_executed {
        sql
        bindings
        time
        connection_name
    }
}

应该返回如下所示的响应

{
  "data": {
    "users": {
      "total": 58,
      "data": [
        {
          "id": 1,
          "username": "john.smith",
          "email": "john.smith@example.com",
          "posts_count": 12
        },
        // ...
      ]
    },
    "queries_executed": [
      {
        "sql": "select count(*) as aggregate from `users`",
        "bindings": "[]",
        "time": 91.07,
        "connection_name": "mysql"
      },
      {
        "sql": "select `users`.`id`, `users`.`username`, `users`.`email`, (select count(*) from `posts` where `users`.`id` = `posts`.`user_id`) as `posts_count` from `users` limit 10 offset 0",
        "bindings": "[]",
        "time": 39.58,
        "connection_name": "mysql"
      }
    ]
  }
}