diff --git a/vendor/magento/module-graph-ql/Controller/GraphQl.php b/vendor/magento/module-graph-ql/Controller/GraphQl.php index f20956407c258..ab766de9d83a6 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 = $this->getDataFromRequest($request); - $result = []; - + $data = []; + $result = null; $schema = null; + $query = ''; + try { + $data = $this->getDataFromRequest($request); + $query = $data['query'] ?? ''; + /** @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'] ?? [] - ); + 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($parsedQuery, $data['variables'] ?? null); + $schema = $this->schemaGenerator->generate(); + + $result = $this->queryProcessor->process( + $schema, + $parsedQuery, + $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 @@ -229,20 +243,25 @@ public function dispatch(RequestInterface $request): ResponseInterface * * @param RequestInterface $request * @return array + * @throws GraphQlInputException */ 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 []; + try { + /** @var Http $request */ + if ($request->isPost()) { + $data = $request->getContent() ? $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 { + $data = []; + } + } catch (\InvalidArgumentException $e) { + throw new GraphQlInputException(__('Unable to parse the request.'), $e); } return $data;