Config.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\GraphQl;
  8. use Magento\Framework\Config\DataInterface;
  9. use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface;
  10. use Magento\Framework\GraphQl\Config\ConfigElementInterface;
  11. use Magento\Framework\GraphQl\Query\Fields as QueryFields;
  12. /**
  13. * Provides access to typing information for a configured GraphQL schema.
  14. */
  15. class Config implements ConfigInterface
  16. {
  17. /**
  18. * @var DataInterface
  19. */
  20. private $configData;
  21. /**
  22. * @var ConfigElementFactoryInterface
  23. */
  24. private $configElementFactory;
  25. /**
  26. * @var QueryFields
  27. */
  28. private $queryFields;
  29. /**
  30. * @param DataInterface $data
  31. * @param ConfigElementFactoryInterface $configElementFactory
  32. * @param QueryFields $queryFields
  33. */
  34. public function __construct(
  35. DataInterface $data,
  36. ConfigElementFactoryInterface $configElementFactory,
  37. QueryFields $queryFields
  38. ) {
  39. $this->configData = $data;
  40. $this->configElementFactory = $configElementFactory;
  41. $this->queryFields = $queryFields;
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function getConfigElement(string $configElementName) : ConfigElementInterface
  47. {
  48. $data = $this->configData->get($configElementName);
  49. if (!isset($data['type'])) {
  50. throw new \LogicException(
  51. sprintf('Config element "%s" is not declared in GraphQL schema', $configElementName)
  52. );
  53. }
  54. $fieldsInQuery = $this->queryFields->getFieldsUsedInQuery();
  55. if (isset($data['fields'])) {
  56. if (!empty($fieldsInQuery)) {
  57. foreach (array_keys($data['fields']) as $fieldName) {
  58. if (!isset($fieldsInQuery[$fieldName])) {
  59. unset($data['fields'][$fieldName]);
  60. }
  61. }
  62. }
  63. ksort($data['fields']);
  64. }
  65. return $this->configElementFactory->createFromConfigData($data);
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function getDeclaredTypes() : array
  71. {
  72. $types = [];
  73. foreach ($this->configData->get(null) as $item) {
  74. if (isset($item['type'])) {
  75. $types[] = [
  76. 'name' => $item['name'],
  77. 'type' => $item['type'],
  78. ];
  79. }
  80. }
  81. return $types;
  82. }
  83. }