response = $response; $this->schemaGenerator = $schemaGenerator; $this->jsonSerializer = $jsonSerializer; $this->queryProcessor = $queryProcessor; $this->graphQlError = $graphQlError; $this->resolverContext = $resolverContext; $this->requestProcessor = $requestProcessor; $this->queryFields = $queryFields; $this->jsonFactory = $jsonFactory ?: ObjectManager::getInstance()->get(JsonFactory::class); $this->httpResponse = $httpResponse ?: ObjectManager::getInstance()->get(HttpResponse::class); $this->contextFactory = $contextFactory ?: ObjectManager::getInstance()->get(ContextFactoryInterface::class); $this->logDataHelper = $logDataHelper ?: ObjectManager::getInstance()->get(LogData::class); $this->loggerPool = $loggerPool ?: ObjectManager::getInstance()->get(LoggerPool::class); $this->areaList = $areaList ?: ObjectManager::getInstance()->get(AreaList::class); $this->queryParser = $queryParser ?: ObjectManager::getInstance()->get(QueryParser::class); } /** * Handle GraphQL request * * @param RequestInterface $request * @return ResponseInterface * @since 100.3.0 */ public function dispatch(RequestInterface $request): ResponseInterface { $this->areaList->getArea(Area::AREA_GRAPHQL)->load(Area::PART_TRANSLATE); $statusCode = 200; $jsonResult = $this->jsonFactory->create(); $data = $this->getDataFromRequest($request); $result = []; $schema = null; try { /** @var Http $request */ $this->requestProcessor->validateRequest($request); $query = $data['query'] ?? ''; $parsedQuery = $this->queryParser->parse($query); $data['parsedQuery'] = $parsedQuery; // We must extract queried field names to avoid instantiation of unnecessary fields in webonyx schema // Temporal coupling is required for performance optimization $this->queryFields->setQuery($parsedQuery, $data['variables'] ?? null); $schema = $this->schemaGenerator->generate(); $result = $this->queryProcessor->process( $schema, $parsedQuery, $this->contextFactory->create(), $data['variables'] ?? [] ); } catch (\Exception $error) { $result['errors'] = isset($result['errors']) ? $result['errors'] : []; $result['errors'][] = $this->graphQlError->create($error); $statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS; } $jsonResult->setHttpResponseCode($statusCode); $jsonResult->setData($result); $jsonResult->renderResult($this->httpResponse); // log information about the query, unless it is an introspection query if (!isset($data['query']) || strpos($data['query'], 'IntrospectionQuery') === false) { $queryInformation = $this->logDataHelper->getLogData($request, $data, $schema, $this->httpResponse); $this->loggerPool->execute($queryInformation); } return $this->httpResponse; } /** * Get data from request body or query string * * @param RequestInterface $request * @return array */ private function getDataFromRequest(RequestInterface $request): array { /** @var Http $request */ if ($request->isPost()) { $data = $this->jsonSerializer->unserialize($request->getContent()); } elseif ($request->isGet()) { $data = $request->getParams(); $data['variables'] = isset($data['variables']) ? $this->jsonSerializer->unserialize($data['variables']) : null; $data['variables'] = is_array($data['variables']) ? $data['variables'] : null; } else { return []; } return $data; } }