QueryProcessor.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\GraphQl\Query;
  8. use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
  9. use Magento\Framework\GraphQl\Schema;
  10. use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
  11. /**
  12. * Wrapper for GraphQl execution of a schema
  13. */
  14. class QueryProcessor
  15. {
  16. /**
  17. * @var ExceptionFormatter
  18. */
  19. private $exceptionFormatter;
  20. /**
  21. * @var QueryComplexityLimiter
  22. */
  23. private $queryComplexityLimiter;
  24. /**
  25. * @param ExceptionFormatter $exceptionFormatter
  26. * @param QueryComplexityLimiter $queryComplexityLimiter
  27. */
  28. public function __construct(
  29. ExceptionFormatter $exceptionFormatter,
  30. QueryComplexityLimiter $queryComplexityLimiter
  31. ) {
  32. $this->exceptionFormatter = $exceptionFormatter;
  33. $this->queryComplexityLimiter = $queryComplexityLimiter;
  34. }
  35. /**
  36. * Process a GraphQl query according to defined schema
  37. *
  38. * @param Schema $schema
  39. * @param string $source
  40. * @param ContextInterface $contextValue
  41. * @param array|null $variableValues
  42. * @param string|null $operationName
  43. * @return Promise|array
  44. */
  45. public function process(
  46. Schema $schema,
  47. string $source,
  48. ContextInterface $contextValue = null,
  49. array $variableValues = null,
  50. string $operationName = null
  51. ) : array {
  52. if (!$this->exceptionFormatter->shouldShowDetail()) {
  53. $this->queryComplexityLimiter->execute();
  54. }
  55. $rootValue = null;
  56. return \GraphQL\GraphQL::executeQuery(
  57. $schema,
  58. $source,
  59. $rootValue,
  60. $contextValue,
  61. $variableValues,
  62. $operationName
  63. )->toArray(
  64. $this->exceptionFormatter->shouldShowDetail() ?
  65. \GraphQL\Error\Debug::INCLUDE_DEBUG_MESSAGE : false
  66. );
  67. }
  68. }