Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/Client/GraphqlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,15 @@ public function query(string $query, GraphqlExecutionContext $context, array $va
);

$context->trace->endpoint = $this->endpoint;
$content = $response->getContent();

$content = $response->getContent(false);

$context->trace->responseSize = \strlen($content);

// TODO
// $result = json_decode(
// $content,
// true,
// flags: JSON_THROW_ON_ERROR
// );
/** @var array{'errors': array<string|int, mixed>|null} $result */
$result = json_decode($content, true, flags: JSON_THROW_ON_ERROR);

// $context->trace->errors = $result['errors'] ?? null;
$context->trace->errors = $result['errors'] ?? null;

return $response->toArray();
}
Expand Down
16 changes: 15 additions & 1 deletion src/DataCollector/GraphqlOrmDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function addQuery(
'endpoint' => $trace->endpoint,
'caller' => $trace->caller,
'response_size' => $trace->responseSize,
'errors' => $trace->errors,
'hydrated_count' => $trace->hydratedCount,
'depth_used' => $trace->depthUsed,
'hydrated_collections' => $trace->hydratedCollections,
Expand All @@ -42,7 +43,7 @@ public function reset(): void
}

/**
* @return array<string, mixed>
* @return array<int, array<string, array<string, mixed>>>
*/
public function getQueries(): array
{
Expand All @@ -64,6 +65,19 @@ public function getTotalDuration(): float
);
}

public function getErrorCount(): int
{
$count = 0;

foreach ($this->getQueries() as $query) {
if (!empty($query['errors'])) {
++$count;
}
}

return $count;
}

public function getName(): string
{
return 'graphql_orm';
Expand Down
32 changes: 18 additions & 14 deletions src/GraphqlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,25 @@ public function execute(

$context->trace->graphql = $graphql;

$result = $this->client
->query(
$graphql,
$context,
$variables
);

$entities = $hydration(
$result,
$context
);
try {
$result = $this->client
->query(
$graphql,
$context,
$variables
);

$this->collector->addQuery(
$context->trace
);
$entities = $hydration(
$result,
$context
);
} catch (\Throwable $e) {
throw $e;
} finally {
$this->collector->addQuery(
$context->trace
);
}

return $entities;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Query/GraphqlQueryTrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ final class GraphqlQueryTrace
public ?array $caller = null;
public ?string $endpoint = null;
public int $responseSize = 0;
// TODO
// /** @var array<string, mixed>|null */
// public ?array $errors = null;
/** @var array<string|int, mixed>|null */
public ?array $errors = null;
public int $hydratedCount = 0;
public int $depthUsed = 0;
public int $hydratedEntities = 0;
Expand Down
79 changes: 68 additions & 11 deletions src/Resources/views/collector/graphql_orm.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@
{% set text %}
<div class="sf-toolbar-info-piece">
<b>Queries</b>
<span>{{ collector.queryCount }}</span>
<span class="sf-toolbar-status sf-toolbar-status-">{{ collector.queryCount }}</span>
</div>
<div class="sf-toolbar-info-piece">
<b>Errors</b>
<span class="sf-toolbar-status sf-toolbar-status-red">{{ collector.errorCount|default(0) }}</span>
</div>
{% endset %}

{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', {
link: profiler_url,
icon: icon,
text: text
text: text,
status: collector.errorCount > 0 ? 'red'
}) }}

{% endblock %}

{# ================= MENU ================= #}

{% block menu %}
<span class="label">
<span class="label {% if collector.errorCount > 0 %}label-status-error{% endif %}">
<span class="icon">
{{ source('@GraphqlOrm/collector/icon.svg') }}
</span>
Expand All @@ -53,7 +58,21 @@
{% else %}
{% for query in collector.queries %}
<div class="sf-profiler-block">
<h3>Query #{{ loop.index }}</h3>
<h3>
Query #{{ loop.index }}
{% if query.errors %}
<span style="
background:#ff6b6b;
color:white;
padding:2px 6px;
border-radius:4px;
font-size:12px;
margin-left:6px;
">
ERROR
</span>
{% endif %}
</h3>
<table>
<tbody>
<tr>
Expand All @@ -71,8 +90,46 @@
</tbody>
</table>

{% if query.errors %}
<h4 style="color: #ff6b6b">GraphQL Errors</h4>

{% for error in query.errors %}
<div style="
background: #2a1416;
border-left: 4px solid #ff6b6b;
padding: 10px;
margin-bottom: 10px;
border-radius: 6px;
">
<strong>{{ error.message }}</strong>

{% if error.extensions.code ?? null %}
<br>
<small>Code : {{ error.extensions.code }}</small>
{% endif %}

{% if error.extensions.exception.stacktrace ?? null %}
<details style="margin-top: 8px">
<summary style="cursor: pointer">
Stacktrace
</summary>
<pre style="
max-height: 250px;
overflow: auto;
background: #18171b;
padding: 10px;
border-radius: 6px;
">
{{ error.extensions.exception.stacktrace|join("\n") }}
</pre>
</details>
{% endif %}
</div>
{% endfor %}
{% endif %}

<h4>Hydration</h4>
<table>
<table {% if query.errors %}style="opacity: .5"{% endif %}>
<tbody>
<tr>
<th style="width:200px">Entities</th>
Expand Down Expand Up @@ -149,15 +206,15 @@ border-radius:6px;
{% endif %}

<script>
function copyGraphql(button){
function copyGraphql(button) {
const query = button.dataset.graphql;
navigator.clipboard.writeText(query)
.then(()=>{
.then(() => {
const original = button.innerText;
button.innerText="Copied ✓";
setTimeout(()=>{
button.innerText=original;
},1200);
button.innerText = "Copied ✓";
setTimeout(() => {
button.innerText = original;
}, 1200);
});
}
</script>
Expand Down