Skip to content

Commit

Permalink
[shopsys] graphql query is not in transaction (#2516)
Browse files Browse the repository at this point in the history
Co-authored-by: Milan Staňo <milan.stano@shopsys.com>
  • Loading branch information
stanoMilan and msshopsys authored Sep 15, 2023
1 parent 57cb595 commit 9397719
Showing 1 changed file with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,41 @@
namespace Shopsys\FrameworkBundle\Component\HttpFoundation;

use Doctrine\ORM\EntityManagerInterface;
use GraphQL\Error\SyntaxError;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\Parser;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

class TransactionalMasterRequestListener
{
protected bool $inTransaction;
protected bool $inTransaction = false;

protected static bool $isManuallyRollbacked = false;

protected const GRAPHQL_ENDPOINT_ROUTE = 'overblog_graphql_endpoint';
protected const QUERY_TYPE = 'query';

/**
* @param \Doctrine\ORM\EntityManagerInterface $em
*/
public function __construct(protected readonly EntityManagerInterface $em)
public function __construct(
protected readonly EntityManagerInterface $em,
) {
}

public static function setTransactionManuallyRollbacked(): void
{
$this->inTransaction = false;
static::$isManuallyRollbacked = true;
}

/**
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event): void
{
if ($event->isMainRequest() && !$this->inTransaction) {
if ($event->isMainRequest() && !$this->inTransaction && !$this->isRequestGraphQlQuery($event)) {
$this->em->beginTransaction();
$this->inTransaction = true;
}
Expand All @@ -37,7 +50,7 @@ public function onKernelRequest(RequestEvent $event): void
*/
public function onKernelResponse(ResponseEvent $event): void
{
if ($event->isMainRequest() && $this->inTransaction) {
if ($event->isMainRequest() && $this->inTransaction && !static::$isManuallyRollbacked) {
$this->em->commit();
$this->inTransaction = false;
}
Expand All @@ -53,4 +66,43 @@ public function onKernelException(ExceptionEvent $event): void
$this->inTransaction = false;
}
}

/**
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $requestEvent
* @return bool
*/
protected function isRequestGraphQlQuery(RequestEvent $requestEvent): bool
{
if ($requestEvent->getRequest()->attributes->get('_route') !== static::GRAPHQL_ENDPOINT_ROUTE) {
return false;
}

$requestContent = $requestEvent->getRequest()->getContent();

if ($requestContent === null || $requestContent === '') {
return false;
}

$source = json_decode($requestContent, true);

if (!array_key_exists(static::QUERY_TYPE, $source)) {
return false;
}

$queryString = $source[static::QUERY_TYPE];

try {
$parsed = Parser::parse($queryString);

Check failure on line 95 in src/Component/HttpFoundation/TransactionalMasterRequestListener.php

View workflow job for this annotation

GitHub Actions / Run checks and tests

Call to static method parse() on an unknown class GraphQL\Language\Parser.

foreach ($parsed->definitions as $definition) {
if ($definition instanceof OperationDefinitionNode) {

Check failure on line 98 in src/Component/HttpFoundation/TransactionalMasterRequestListener.php

View workflow job for this annotation

GitHub Actions / Run checks and tests

Class GraphQL\Language\AST\OperationDefinitionNode not found.
return $definition->operation === static::QUERY_TYPE;

Check failure on line 99 in src/Component/HttpFoundation/TransactionalMasterRequestListener.php

View workflow job for this annotation

GitHub Actions / Run checks and tests

Access to property $operation on an unknown class GraphQL\Language\AST\OperationDefinitionNode.
}
}
} catch (SyntaxError) {

Check failure on line 102 in src/Component/HttpFoundation/TransactionalMasterRequestListener.php

View workflow job for this annotation

GitHub Actions / Run checks and tests

Caught class GraphQL\Error\SyntaxError not found.
return false;
}

return false;
}
}

0 comments on commit 9397719

Please sign in to comment.