Skip to content

Commit

Permalink
feat: add support for barryvdh/laravel-debugbar (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
emil-nasso authored and wecc committed Sep 14, 2018
1 parent 9ef2bb3 commit 2d2ebc5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Decorate the response with debug information when using [barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar).

### Changed
- Upgrade webonyx/[email protected] for improved schema language support.

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,17 @@ php artisan vendor:publish

- `BUTLER_GRAPHQL_INCLUDE_DEBUG_MESSAGE` – Set to `true` to include the real error message in error responses. Defaults to `false`.
- `BUTLER_GRAPHQL_INCLUDE_TRACE` – Set to `true` to include stack traces in error responses. Defaults to `false`.

#### Debugbar

Butler GraphQL has support for automatically decorating responses with additional debug information when using [laravel-debugbar](https://github.com/barryvdh/laravel-debugbar). Details such as database queries and memory usage will automatically be available in the response _if barryvdh/laravel-debugbar is installed_.

To install and activate it, simply install `barryvdh/laravel-debugbar` as a `require-dev` dependency.

```
composer require barryvdh/laravel-debugbar --dev
```

When installed, make sure that `APP_DEBUG` is set to `true`, that's it.

Customizing what data to collect and include in the response is easily done by copying the [default config file](https://github.com/barryvdh/laravel-debugbar/blob/master/config/debugbar.php) to `config/debugbar.php` and adjust as needed.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
{
"name": "Christoffer Persson",
"email": "[email protected]"
},
{
"name": "Emil Andersson",
"email": "[email protected]"
}
],
"autoload": {
Expand Down Expand Up @@ -46,5 +50,8 @@
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^7.0"
},
"suggest": {
"barryvdh/laravel-debugbar": "View debug information in responses."
},
"type": "library"
}
13 changes: 12 additions & 1 deletion src/Concerns/HandlesGraphqlRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __invoke(Request $request)

$result->setErrorFormatter([$this, 'errorFormatter']);

return $result->toArray($this->debugFlags());
return $this->decorateResponse($result->toArray($this->debugFlags()));
}

public function errorFormatter(GraphqlError $graphqlError)
Expand Down Expand Up @@ -197,4 +197,15 @@ public function typesNamespace(): string
{
return $this->namespace() . 'Types\\';
}

public function decorateResponse(array $data): array
{
if (
app()->bound('debugbar') &&
app('debugbar')->isEnabled()
) {
$data['debug'] = app('debugbar')->getData();
}
return $data;
}
}
35 changes: 35 additions & 0 deletions tests/HandlesGraphqlRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,39 @@ public function test_invalid_query()
'query' => 'hello world'
]));
}

public function test_without_debugbar()
{
$controller = $this->app->make(GraphqlController::class);
$data = $controller(Request::create('/', 'POST', [
'query' => 'query { ping }',
]));
$this->assertSame(['data' => ['ping' => 'pong']], $data);
}

public function test_with_debugbar()
{
$debugBar = Mockery::mock(\stdClass::class);
$debugBar->shouldReceive('isEnabled')->once()->andReturnTrue();
$debugBar->shouldReceive('getData')->once()->andReturn(['queries' => 10]);

$this->app->instance('debugbar', $debugBar);

$controller = $this->app->make(GraphqlController::class);
$data = $controller(Request::create('/', 'POST', [
'query' => 'query { ping }',
]));

$this->assertSame(
[
'data' => [
'ping' => 'pong',
],
'debug' => [
'queries' => 10,
]
],
$data
);
}
}
11 changes: 11 additions & 0 deletions tests/stubs/Queries/Ping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Butler\Graphql\Tests\Queries;

class Ping
{
public function __invoke($root, $args, $context)
{
return "pong";
}
}
1 change: 1 addition & 0 deletions tests/stubs/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Query {
throwException: String!
throwModelNotFoundException: String!
throwValidationException: String!
ping: String!
}

type Mutation {
Expand Down

0 comments on commit 2d2ebc5

Please sign in to comment.