Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Fields.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use GraphQL\Language\AST\Node;
11 use GraphQL\Language\AST\NodeKind;
12 
16 class Fields
17 {
21  private $fieldsUsedInQuery = [];
22 
29  public function setQuery($query)
30  {
31  $queryFields = [];
32  try {
33  $queryAst = \GraphQL\Language\Parser::parse(new \GraphQL\Language\Source($query ?: '', 'GraphQL'));
34  \GraphQL\Language\Visitor::visit(
35  $queryAst,
36  [
37  'leave' => [
38  NodeKind::NAME => function (Node $node) use (&$queryFields) {
39  $queryFields[$node->value] = $node->value;
40  }
41  ]
42  ]
43  );
44  } catch (\Exception $e) {
45  // If a syntax error is encountered do not collect fields
46  }
47  if (isset($queryFields['IntrospectionQuery'])) {
48  // It must be possible to query any fields during introspection query
49  $queryFields = [];
50  }
51  $this->fieldsUsedInQuery = $queryFields;
52  }
53 
61  public function getFieldsUsedInQuery()
62  {
63  return $this->fieldsUsedInQuery;
64  }
65 }