From f5e2a49f5589480fd1a2ace88bd47958db80337e Mon Sep 17 00:00:00 2001 From: HECHT Axel Date: Mon, 23 Feb 2026 21:34:58 +0100 Subject: [PATCH] feat: add GraphQL errors in profiler --- src/Client/GraphqlClient.php | 13 ++- src/DataCollector/GraphqlOrmDataCollector.php | 16 +++- src/GraphqlManager.php | 32 ++++---- src/Query/GraphqlQueryTrace.php | 5 +- .../views/collector/graphql_orm.html.twig | 79 ++++++++++++++++--- 5 files changed, 108 insertions(+), 37 deletions(-) diff --git a/src/Client/GraphqlClient.php b/src/Client/GraphqlClient.php index 9ff0018..96ca0e1 100644 --- a/src/Client/GraphqlClient.php +++ b/src/Client/GraphqlClient.php @@ -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|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(); } diff --git a/src/DataCollector/GraphqlOrmDataCollector.php b/src/DataCollector/GraphqlOrmDataCollector.php index d8cecf2..f805cae 100644 --- a/src/DataCollector/GraphqlOrmDataCollector.php +++ b/src/DataCollector/GraphqlOrmDataCollector.php @@ -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, @@ -42,7 +43,7 @@ public function reset(): void } /** - * @return array + * @return array>> */ public function getQueries(): array { @@ -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'; diff --git a/src/GraphqlManager.php b/src/GraphqlManager.php index 743e2f7..0e0d51a 100644 --- a/src/GraphqlManager.php +++ b/src/GraphqlManager.php @@ -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; } diff --git a/src/Query/GraphqlQueryTrace.php b/src/Query/GraphqlQueryTrace.php index 908bb6a..531ac28 100644 --- a/src/Query/GraphqlQueryTrace.php +++ b/src/Query/GraphqlQueryTrace.php @@ -18,9 +18,8 @@ final class GraphqlQueryTrace public ?array $caller = null; public ?string $endpoint = null; public int $responseSize = 0; - // TODO - // /** @var array|null */ - // public ?array $errors = null; + /** @var array|null */ + public ?array $errors = null; public int $hydratedCount = 0; public int $depthUsed = 0; public int $hydratedEntities = 0; diff --git a/src/Resources/views/collector/graphql_orm.html.twig b/src/Resources/views/collector/graphql_orm.html.twig index 2f9f08e..64f821c 100644 --- a/src/Resources/views/collector/graphql_orm.html.twig +++ b/src/Resources/views/collector/graphql_orm.html.twig @@ -13,14 +13,19 @@ {% set text %}
Queries - {{ collector.queryCount }} + {{ collector.queryCount }} +
+
+ Errors + {{ collector.errorCount|default(0) }}
{% endset %} {{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: profiler_url, icon: icon, - text: text + text: text, + status: collector.errorCount > 0 ? 'red' }) }} {% endblock %} @@ -28,7 +33,7 @@ {# ================= MENU ================= #} {% block menu %} - + {{ source('@GraphqlOrm/collector/icon.svg') }} @@ -53,7 +58,21 @@ {% else %} {% for query in collector.queries %}
-

Query #{{ loop.index }}

+

+ Query #{{ loop.index }} + {% if query.errors %} + + ERROR + + {% endif %} +

@@ -71,8 +90,46 @@
+ {% if query.errors %} +

GraphQL Errors

+ + {% for error in query.errors %} +
+ {{ error.message }} + + {% if error.extensions.code ?? null %} +
+ Code : {{ error.extensions.code }} + {% endif %} + + {% if error.extensions.exception.stacktrace ?? null %} +
+ + Stacktrace + +
+{{ error.extensions.exception.stacktrace|join("\n") }}
+                                    
+
+ {% endif %} +
+ {% endfor %} + {% endif %} +

Hydration

- +
@@ -149,15 +206,15 @@ border-radius:6px; {% endif %}
Entities