Expand my Community achievements bar.

SOLVED

Getting a Custom Object from CIF's Product class

Avatar

Level 1

Hi!

 

There's a Product in Magento which has a custom attribute. If I run this GraphQL:

{products(filter: { sku: { eq: "HAMC8489B717" } }) {items {name product_performances}}}

I get this answer:

{
  "data": {
    "products": {
      "items": [
        {
          "name": "252",
          "product_performances": "[{\"record_id\":\"0\",\"title\":\"Performance Title1\",\"description\":\"Performance Description\",\"icon\":[{\"type\":\"image\\/jpeg\",\"name\":\"A33A8D44-E98C-4C95-94B9-3CA1A29ECEF9.jpeg\",\"size\":\"346408\",\"url\":\"\\/media\\/catalog\\/category\\/A33A8D44-E98C-4C95-94B9-3CA1A29ECEF9.jpeg\",\"previewType\":\"image\",\"id\":\"QTMzQThENDQtRTk4Qy00Qzk1LTk0QjktM0NBMUEyOUVDRUY5LmpwZWc,\"}]},{\"record_id\":\"1\",\"title\":\"Performance Title2\",\"description\":\"Performance Description2\",\"icon\":[{\"type\":\"image\\/jpeg\",\"name\":\"image_resized_card.jpeg\",\"size\":\"43256\",\"url\":\"\\/media\\/catalog\\/category\\/image_resized_card.jpeg\",\"previewType\":\"image\",\"id\":\"aW1hZ2VfcmVzaXplZF9jYXJkLmpwZWc,\"}]}]"
        }
      ]
    }
  }
}

In my code I'm doing this:

@Self
@Via(type= ResourceSuperType.class)
private Product product;

@PostConstruct
private void init() {
AbstractProductRetriever productRetriever = product.getProductRetriever();
if (Objects.nonNull(productRetriever)) {
CustomFieldQueryDefinition queryDefinition = q -> q.addCustomSimpleField("title");
productRetriever.extendProductQueryWith(q -> q.addCustomObjectField(PRODUCT_PERFORMANCES, queryDefinition));
}
}

The Product Retriever is not being null but if I tried to do something product I get a NPE:

 

at com.adobe.cq.commerce.core.components.models.retriever.AbstractProductRetriever.populate(AbstractProductRetriever.java:217)

 

If I remove the two lines in the if statement, no exception is thrown.

 

What's the correct way to retrieve data from a Custom Object in CIF?

 

Thanks

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
1 Reply

Avatar

Correct answer by
Community Advisor