response = $response; $this->schemaGenerator = $schemaGenerator; $this->jsonSerializer = $jsonSerializer; $this->queryProcessor = $queryProcessor; $this->graphQlError = $graphQlError; $this->resolverContext = $resolverContext; $this->requestProcessor = $requestProcessor; $this->queryFields = $queryFields; } /** * Handle GraphQL request * * @param RequestInterface $request * @return ResponseInterface * @since 100.3.0 */ public function dispatch(RequestInterface $request) : ResponseInterface { $statusCode = 200; try { /** @var Http $request */ $this->requestProcessor->processHeaders($request); $data = $this->jsonSerializer->unserialize($request->getContent()); $query = isset($data['query']) ? $data['query'] : ''; $variables = isset($data['variables']) ? $data['variables'] : null; // We have to extract queried field names to avoid instantiation of non necessary 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->resolverContext, isset($data['variables']) ? $data['variables'] : [] ); } catch (\Exception $error) { $result['errors'] = isset($result) && isset($result['errors']) ? $result['errors'] : []; $result['errors'][] = $this->graphQlError->create($error); $statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS; } $this->response->setBody($this->jsonSerializer->serialize($result))->setHeader( 'Content-Type', 'application/json' )->setHttpResponseCode($statusCode); return $this->response; } }