InterfaceFactory.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Config\Element;
  8. use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface;
  9. use Magento\Framework\GraphQl\Config\ConfigElementInterface;
  10. use Magento\Framework\ObjectManagerInterface;
  11. /**
  12. * Factory for config elements of 'interface' type.
  13. */
  14. class InterfaceFactory implements ConfigElementFactoryInterface
  15. {
  16. /**
  17. * @var ObjectManagerInterface
  18. */
  19. private $objectManager;
  20. /**
  21. * @var ArgumentFactory
  22. */
  23. private $argumentFactory;
  24. /**
  25. * @var FieldFactory
  26. */
  27. private $fieldFactory;
  28. /**
  29. * @param ObjectManagerInterface $objectManager
  30. * @param ArgumentFactory $argumentFactory
  31. * @param FieldFactory $fieldFactory
  32. */
  33. public function __construct(
  34. ObjectManagerInterface $objectManager,
  35. ArgumentFactory $argumentFactory,
  36. FieldFactory $fieldFactory
  37. ) {
  38. $this->objectManager = $objectManager;
  39. $this->argumentFactory = $argumentFactory;
  40. $this->fieldFactory = $fieldFactory;
  41. }
  42. /**
  43. * Instantiate an object representing 'interface' GraphQL config element.
  44. */
  45. public function createFromConfigData(array $data): ConfigElementInterface
  46. {
  47. $fields = [];
  48. foreach ($data['fields'] as $field) {
  49. $arguments = [];
  50. foreach ($field['arguments'] as $argument) {
  51. $arguments[$argument['name']] = $this->argumentFactory->createFromConfigData($argument);
  52. }
  53. $fields[$field['name']] = $this->fieldFactory->createFromConfigData($field, $arguments);
  54. }
  55. return $this->create($data, $fields);
  56. }
  57. /**
  58. * Create interface object based off array of configured GraphQL Output/InputInterface.
  59. *
  60. * Interface data must contain name, type resolver, and field definitions. The type resolver should point to an
  61. * implementation of the TypeResolverInterface that decides what concrete GraphQL type to output. Description is
  62. * the only optional field.
  63. *
  64. * @param array $interfaceData
  65. * @param array $fields
  66. * @return InterfaceType
  67. */
  68. public function create(
  69. array $interfaceData,
  70. array $fields
  71. ) : InterfaceType {
  72. return $this->objectManager->create(
  73. InterfaceType::class,
  74. [
  75. 'name' => $interfaceData['name'],
  76. 'typeResolver' => $interfaceData['typeResolver'],
  77. 'fields' => $fields,
  78. 'description' => isset($interfaceData['description']) ? $interfaceData['description'] : ''
  79. ]
  80. );
  81. }
  82. }