|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Graycore\GraphQlIntrospectionCache\Executor; |
| 4 | + |
| 5 | +use GraphQL\Executor\ExecutionContext; |
| 6 | +use GraphQL\Executor\ExecutionResult; |
| 7 | +use GraphQL\Executor\ExecutorImplementation; |
| 8 | +use GraphQL\Executor\Promise\Promise; |
| 9 | +use GraphQL\Executor\Promise\PromiseAdapter; |
| 10 | +use GraphQL\Language\AST\DocumentNode; |
| 11 | +use GraphQL\Type\Schema; |
| 12 | +use GraphQL\Utils\Utils; |
| 13 | +use Magento\Framework\App\ObjectManager; |
| 14 | +use ReflectionMethod; |
| 15 | +use ReflectionProperty; |
| 16 | +use SplObjectStorage; |
| 17 | + |
| 18 | +class ReferenceExecutor extends \GraphQL\Executor\ReferenceExecutor |
| 19 | +{ |
| 20 | + private static ?self $executorInstance = null; |
| 21 | + |
| 22 | + protected function __construct(ExecutionContext $context) |
| 23 | + { |
| 24 | + if (is_callable('parent::__construct')) { |
| 25 | + parent::__construct($context); |
| 26 | + } else { |
| 27 | + $this->setExecutorPrivateProp('UNDEFINED', Utils::undefined(), true); |
| 28 | + $this->setExecutorPrivateProp('exeContext', $context); |
| 29 | + $this->setExecutorPrivateProp('subFieldCache', new SplObjectStorage()); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private function setExecutorPrivateProp(string $property, $value, bool $static = false): void |
| 34 | + { |
| 35 | + try { |
| 36 | + $reflectionProperty = new ReflectionProperty(\GraphQL\Executor\ReferenceExecutor::class, $property); |
| 37 | + $reflectionProperty->setAccessible(true); |
| 38 | + $reflectionProperty->setValue($static ? null : $this, $value); |
| 39 | + } catch (\ReflectionException $e) {} |
| 40 | + } |
| 41 | + |
| 42 | + public static function create( |
| 43 | + PromiseAdapter $promiseAdapter, |
| 44 | + Schema $schema, |
| 45 | + DocumentNode $documentNode, |
| 46 | + $rootValue, |
| 47 | + $contextValue, |
| 48 | + $variableValues, |
| 49 | + ?string $operationName, |
| 50 | + callable $fieldResolver |
| 51 | + ) : ExecutorImplementation { |
| 52 | + if (self::$executorInstance !== null) { |
| 53 | + return self::$executorInstance; |
| 54 | + } |
| 55 | + |
| 56 | + $reflectionMethod = new ReflectionMethod(\GraphQL\Executor\ReferenceExecutor::class, 'buildExecutionContext'); |
| 57 | + if ($reflectionMethod->isPrivate()) { |
| 58 | + $reflectionMethod->setAccessible(true); |
| 59 | + } |
| 60 | + |
| 61 | + $exeContext = $reflectionMethod->invoke( |
| 62 | + null, |
| 63 | + $schema, |
| 64 | + $documentNode, |
| 65 | + $rootValue, |
| 66 | + $contextValue, |
| 67 | + $variableValues, |
| 68 | + $operationName, |
| 69 | + $fieldResolver, |
| 70 | + $promiseAdapter |
| 71 | + ); |
| 72 | + |
| 73 | + if (is_array($exeContext)) { |
| 74 | + $promise = $promiseAdapter->createFulfilled(new ExecutionResult(null, $exeContext)); |
| 75 | + return new class($promise) implements ExecutorImplementation |
| 76 | + { |
| 77 | + private Promise $result; |
| 78 | + |
| 79 | + public function __construct(Promise $result) |
| 80 | + { |
| 81 | + $this->result = $result; |
| 82 | + } |
| 83 | + |
| 84 | + public function doExecute() : Promise |
| 85 | + { |
| 86 | + return $this->result; |
| 87 | + } |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + return self::$executorInstance = ObjectManager::getInstance()->create( |
| 92 | + ReferenceExecutor::class, |
| 93 | + ['context' => $exeContext] |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments