Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GraphQl.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
21 
28 {
32  private $response;
33 
37  private $schemaGenerator;
38 
42  private $jsonSerializer;
43 
47  private $queryProcessor;
48 
52  private $graphQlError;
53 
57  private $resolverContext;
58 
62  private $requestProcessor;
63 
67  private $queryFields;
68 
79  public function __construct(
80  Response $response,
81  SchemaGeneratorInterface $schemaGenerator,
82  SerializerInterface $jsonSerializer,
83  QueryProcessor $queryProcessor,
84  ExceptionFormatter $graphQlError,
85  ContextInterface $resolverContext,
86  HttpRequestProcessor $requestProcessor,
87  QueryFields $queryFields
88  ) {
89  $this->response = $response;
90  $this->schemaGenerator = $schemaGenerator;
91  $this->jsonSerializer = $jsonSerializer;
92  $this->queryProcessor = $queryProcessor;
93  $this->graphQlError = $graphQlError;
94  $this->resolverContext = $resolverContext;
95  $this->requestProcessor = $requestProcessor;
96  $this->queryFields = $queryFields;
97  }
98 
106  {
107  $statusCode = 200;
108  try {
110  $this->requestProcessor->processHeaders($request);
111  $data = $this->jsonSerializer->unserialize($request->getContent());
112 
113  $query = isset($data['query']) ? $data['query'] : '';
114 
115  // We have to extract queried field names to avoid instantiation of non necessary fields in webonyx schema
116  // Temporal coupling is required for performance optimization
117  $this->queryFields->setQuery($query);
118  $schema = $this->schemaGenerator->generate();
119 
120  $result = $this->queryProcessor->process(
121  $schema,
122  $query,
123  $this->resolverContext,
124  isset($data['variables']) ? $data['variables'] : []
125  );
126  } catch (\Exception $error) {
127  $result['errors'] = isset($result) && isset($result['errors']) ? $result['errors'] : [];
128  $result['errors'][] = $this->graphQlError->create($error);
130  }
131  $this->response->setBody($this->jsonSerializer->serialize($result))->setHeader(
132  'Content-Type',
133  'application/json'
134  )->setHttpResponseCode($statusCode);
135  return $this->response;
136  }
137 }
$response
Definition: 404.php:11
__construct(Response $response, SchemaGeneratorInterface $schemaGenerator, SerializerInterface $jsonSerializer, QueryProcessor $queryProcessor, ExceptionFormatter $graphQlError, ContextInterface $resolverContext, HttpRequestProcessor $requestProcessor, QueryFields $queryFields)
Definition: GraphQl.php:79