diff --git a/vendor/magento/module-graph-ql/Controller/GraphQl.php b/vendor/magento/module-graph-ql/Controller/GraphQl.php index f78d3d76600ce..4ec008a630668 100644 --- a/vendor/magento/module-graph-ql/Controller/GraphQl.php +++ b/vendor/magento/module-graph-ql/Controller/GraphQl.php @@ -1,14 +1,14 @@ jsonFactory->create(); $data = []; - $result = []; - + $result = null; $schema = null; + $query = ''; + try { $data = $this->getDataFromRequest($request); + $query = $data['query'] ?? ''; /** @var Http $request */ $this->requestProcessor->validateRequest($request); - - $query = $data['query'] ?? ''; - $variables = $data['variables'] ?? null; - - $this->queryParser->parse($query); - - // 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($query, $variables); - $schema = $this->schemaGenerator->generate(); - - $result = $this->queryProcessor->process( - $schema, - $query, - $this->contextFactory->create(), - $data['variables'] ?? [] - ); + if ($request->isGet() || $request->isPost()) { + $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($query, $data['variables'] ?? null); + $schema = $this->schemaGenerator->generate(); + + $result = $this->queryProcessor->process( + $schema, + $query, + $this->contextFactory->create(), + $data['variables'] ?? [] + ); + } + } catch (SyntaxError|GraphQlInputException $error) { + $result = [ + 'errors' => [FormattedError::createFromException($error)], + ]; + $statusCode = 400; } catch (\Exception $error) { - $result['errors'] = isset($result['errors']) ? $result['errors'] : []; - $result['errors'][] = $this->graphQlError->create($error); + $result = [ + 'errors' => [$this->graphQlError->create($error)], + ]; $statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS; } $jsonResult->setHttpResponseCode($statusCode); - $jsonResult->setData($result); + if ($result !== null) { + $jsonResult->setData($result); + } $jsonResult->renderResult($this->httpResponse); // log information about the query, unless it is an introspection query @@ -248,6 +257,7 @@ public function dispatch(RequestInterface $request): ResponseInterface * * @param RequestInterface $request * @return array + * @throws GraphQlInputException */ private function getDataFromRequest(RequestInterface $request): array {