Options.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\ConfigurableProductGraphQl\Model\Resolver;
  8. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  9. use Magento\Catalog\Api\Data\ProductInterface;
  10. use Magento\ConfigurableProduct\Model\Product\Type\Configurable as Type;
  11. use Magento\ConfigurableProductGraphQl\Model\Options\Collection as OptionCollection;
  12. use Magento\Framework\EntityManager\MetadataPool;
  13. use Magento\Framework\GraphQl\Config\Element\Field;
  14. use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
  15. use Magento\Framework\GraphQl\Query\ResolverInterface;
  16. /**
  17. * @inheritdoc
  18. */
  19. class Options implements ResolverInterface
  20. {
  21. /**
  22. * @var OptionCollection
  23. */
  24. private $optionCollection;
  25. /**
  26. * @var ValueFactory
  27. */
  28. private $valueFactory;
  29. /**
  30. * @var MetadataPool
  31. */
  32. private $metadataPool;
  33. /**
  34. * @param OptionCollection $optionCollection
  35. * @param ValueFactory $valueFactory
  36. * @param MetadataPool $metadataPool
  37. */
  38. public function __construct(
  39. OptionCollection $optionCollection,
  40. ValueFactory $valueFactory,
  41. MetadataPool $metadataPool
  42. ) {
  43. $this->optionCollection = $optionCollection;
  44. $this->valueFactory = $valueFactory;
  45. $this->metadataPool = $metadataPool;
  46. }
  47. /**
  48. * Fetch and format configurable variants.
  49. *
  50. * {@inheritdoc}
  51. */
  52. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  53. {
  54. $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
  55. if ($value['type_id'] !== Type::TYPE_CODE || !isset($value[$linkField])) {
  56. $result = function () {
  57. return null;
  58. };
  59. return $this->valueFactory->create($result);
  60. }
  61. $this->optionCollection->addProductId((int)$value[$linkField]);
  62. $result = function () use ($value, $linkField) {
  63. return $this->optionCollection->getAttributesByProductId((int)$value[$linkField]);
  64. };
  65. return $this->valueFactory->create($result);
  66. }
  67. }