66
77namespace Magento \CatalogGraphQl \Model \Config ;
88
9+ use Magento \Catalog \Model \Product ;
10+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
11+ use Magento \CatalogGraphQl \Model \Resolver \Products \Attributes \Collection ;
12+ use Magento \EavGraphQl \Model \Resolver \Query \Type ;
13+ use Magento \Framework \App \Config \ScopeConfigInterface ;
914use Magento \Framework \Config \ReaderInterface ;
1015use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
1116use Magento \Framework \GraphQl \Schema \Type \Entity \MapperInterface ;
1217use Magento \Framework \Reflection \TypeProcessor ;
13- use Magento \EavGraphQl \Model \Resolver \Query \Type ;
14- use Magento \CatalogGraphQl \Model \Resolver \Products \Attributes \Collection ;
18+ use Magento \Store \Model \ScopeInterface ;
1519
1620/**
1721 * Adds custom/eav attribute to Catalog product types in the GraphQL config.
1822 */
1923class AttributeReader implements ReaderInterface
2024{
25+ public const XML_PATH_INCLUDE_DYNAMIC_ATTRIBUTES =
26+ 'web_api/graphql/include_dynamic_attributes_as_entity_type_fields ' ;
27+
2128 /**
2229 * @var MapperInterface
2330 */
24- private $ mapper ;
31+ private MapperInterface $ mapper ;
2532
2633 /**
2734 * @var Type
2835 */
29- private $ typeLocator ;
36+ private Type $ typeLocator ;
3037
3138 /**
3239 * @var Collection
3340 */
34- private $ collection ;
41+ private Collection $ collection ;
42+
43+ /**
44+ * @var ScopeConfigInterface
45+ */
46+ private ScopeConfigInterface $ config ;
3547
3648 /**
3749 * @param MapperInterface $mapper
3850 * @param Type $typeLocator
3951 * @param Collection $collection
52+ * @param ScopeConfigInterface $config
4053 */
4154 public function __construct (
4255 MapperInterface $ mapper ,
4356 Type $ typeLocator ,
44- Collection $ collection
57+ Collection $ collection ,
58+ ScopeConfigInterface $ config
4559 ) {
4660 $ this ->mapper = $ mapper ;
4761 $ this ->typeLocator = $ typeLocator ;
4862 $ this ->collection = $ collection ;
63+ $ this ->config = $ config ;
4964 }
5065
5166 /**
@@ -58,22 +73,25 @@ public function __construct(
5873 */
5974 public function read ($ scope = null ) : array
6075 {
61- $ typeNames = $ this ->mapper ->getMappedTypes (\Magento \Catalog \Model \Product::ENTITY );
62- $ config =[];
63- /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
64- foreach ($ this ->collection ->getAttributes () as $ attribute ) {
65- $ attributeCode = $ attribute ->getAttributeCode ();
66- $ locatedType = $ this ->typeLocator ->getType (
67- $ attributeCode ,
68- \Magento \Catalog \Model \Product::ENTITY
69- ) ?: 'String ' ;
70- $ locatedType = $ locatedType === TypeProcessor::NORMALIZED_ANY_TYPE ? 'String ' : ucfirst ($ locatedType );
71- foreach ($ typeNames as $ typeName ) {
72- $ config [$ typeName ]['fields ' ][$ attributeCode ] = [
73- 'name ' => $ attributeCode ,
74- 'type ' => $ locatedType ,
75- 'arguments ' => []
76- ];
76+ $ config = [];
77+
78+ if ($ this ->config ->isSetFlag (self ::XML_PATH_INCLUDE_DYNAMIC_ATTRIBUTES , ScopeInterface::SCOPE_STORE )) {
79+ $ typeNames = $ this ->mapper ->getMappedTypes (Product::ENTITY );
80+
81+ /** @var Attribute $attribute */
82+ foreach ($ this ->collection ->getAttributes () as $ attribute ) {
83+ $ attributeCode = $ attribute ->getAttributeCode ();
84+ $ locatedType = $ this ->typeLocator ->getType ($ attributeCode , Product::ENTITY ) ?: 'String ' ;
85+ $ locatedType = TypeProcessor::NORMALIZED_ANY_TYPE === $ locatedType ? 'String ' : ucfirst ($ locatedType );
86+
87+ foreach ($ typeNames as $ typeName ) {
88+ $ config [$ typeName ]['fields ' ][$ attributeCode ] = [
89+ 'name ' => $ attributeCode ,
90+ 'type ' => $ locatedType ,
91+ 'arguments ' => [],
92+ 'deprecated ' => ['reason ' => 'Use the `custom_attributes` field instead. ' ],
93+ ];
94+ }
7795 }
7896 }
7997
0 commit comments