thecodingmachine / graphqlite-bundle

A Symfony bundle for thecodingmachine/graphqlite.
35 stars 39 forks source link

Events #90

Closed MattBred closed 1 year ago

MattBred commented 3 years ago

Are there any plans on dispatching events in the bundle?

For example, I want to add middleware that logs anytime a graphql response has errors. I know I could add a kernel response listener and parse the response, but I feel like it would be easier to use if there was a built-in.

If we were to add a full event system, what ones would be useful?

This is my proof of concept:


<?php
diff --git a/vendor/thecodingmachine/graphqlite-bundle/Controller/GraphqliteController.php b/vendor/thecodingmachine/graphqlite-bundle/Controller/GraphqliteController.php
index 0558090ea1023046914572691c01e2f8b22913de..0a9127c6cf5b51f673a9fd22565d0e82fb22eb84 100644
--- a/vendor/thecodingmachine/graphqlite-bundle/Controller/GraphqliteController.php
+++ b/vendor/thecodingmachine/graphqlite-bundle/Controller/GraphqliteController.php
@@ -9,6 +9,7 @@ use Laminas\Diactoros\ServerRequestFactory;
 use Laminas\Diactoros\StreamFactory;
 use Laminas\Diactoros\UploadedFileFactory;
 use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
+use TheCodingMachine\Graphqlite\Bundle\Event\GraphqliteRequestErrorsEvent;
 use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
 use function array_map;
 use GraphQL\Error\Debug;
@@ -21,6 +22,7 @@ use function json_decode;
 use Psr\Http\Message\ServerRequestInterface;
 use RuntimeException;
 use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
@@ -37,6 +39,8 @@ class GraphqliteController
      * @var HttpMessageFactoryInterface
      */
     private $httpMessageFactory;
+    /** @var EventDispatcherInterface */
+    private $dispatcher;
     /** @var bool|int */
     private $debug;
     /**
@@ -44,9 +48,10 @@ class GraphqliteController
      */
     private $serverConfig;

-    public function __construct(ServerConfig $serverConfig, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = null)
+    public function __construct(ServerConfig $serverConfig, EventDispatcherInterface $dispatcher, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = null)
     {
         $this->serverConfig = $serverConfig;
+        $this->dispatcher = $dispatcher;
         $this->httpMessageFactory = $httpMessageFactory ?: new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
         $this->debug = $debug ?? $serverConfig->getDebug();
     }
@@ -100,6 +105,9 @@ class GraphqliteController

         $httpCodeDecider = new HttpCodeDecider();
         if ($result instanceof ExecutionResult) {
+            if (!empty($result->errors)) {
+                $this->dispatcher->dispatch(new GraphqliteRequestErrorsEvent($result->errors));
+            }
             return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result));
         }
         if (is_array($result)) {
diff --git a/vendor/thecodingmachine/graphqlite-bundle/Event/GraphqliteRequestErrorsEvent.php b/vendor/thecodingmachine/graphqlite-bundle/Event/GraphqliteRequestErrorsEvent.php
new file mode 100644
index 0000000000000000000000000000000000000000..2bf28665d5e7d5bf2867dd9f7bd937b09acaa15c
--- /dev/null
+++ b/vendor/thecodingmachine/graphqlite-bundle/Event/GraphqliteRequestErrorsEvent.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace TheCodingMachine\Graphqlite\Bundle\Event;
+
+use GraphQL\Error\Error;
+use Psr\Container\ContainerInterface;
+use Symfony\Component\EventDispatcher\Event;
+
+class GraphqliteRequestErrorsEvent extends Event
+{
+    /** @var Error[] */
+    private $errors;
+
+    public function __construct(array $errors) {
+        $this->errors = $errors;
+    }
+
+    /**
+    * @return Error[]
+    */
+    public function getErrors(): array {
+        return $this->errors;
+    }
+}
nguyenk commented 1 year ago

This issue is stale because it has been open 6 month with no activity. You can reopen it if the issue is still happening.