Expand my Community achievements bar.

Mark Solution

This conversation has been locked due to inactivity. Please create a new post.

SOLVED

How to extend graphql query search arguments

Avatar

Level 2

Hi,

 

I'm trying to extend the CIF product list component but I have some problems in customizing the GraphQL query.

 

I've extended the ProductList model and added custom fields in the query as explained also in this video and guide: 

I have also the need to modify the query search arguments with a custom criteria, but I really can't find a guide or an example to do this without having to rewrite the entire component model.

 

To be more specific, I need to edit the "searchArgs" that are here (SearchResultsServiceImpl) used to generate the graphQL query:

 

Operations.query(query -> query.products(searchArgs, queryArgs)).toString();

 

Do I need to write a custom function like "fetchProducts" and build from scratch a product retriever?

Or is there a way to customize the existing query like I've done for the returned attribute set with 

.addCustomObjectField and .addCustomSimpleField

?

 

To achieve what I need I've edited the CIF components source code and it works, but it seems like an extreme solution to a maybe more simpler problem

 

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hi @SantoshSai ,

 

thank you, I've tried this way extending the ProductList model successfully but the query is executed inside the SearchResultsServiceImpl class that is in fact a Service, so I would have to override the whole "performQuery" method.

Instead I've found this project https://github.com/adobe/graphql-java-generator with which I can define a precise magento schema to generate custom Java classes. With those classes I can rewrite the ProductList model generating my custom graphql query. I know it is not an extension of the existing models and it will require some time initially, but maybe is a solution I can re-use with other custom components in the same project.

 

Thank you anyway and I hope I've chosen a right solution for this problem.

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @Guglielmo89 ,

Yes, It is recommended to customize and extending core component according to your requirement - similar to the the way you edited source code in CIF component (but to extend rather than changing in core). However, best recommendation is -  whenever possible using existing CIF core component as a starting point for your customization.

  • Sling Model delegation - which you can use to customize existing core component.

Based on this you can update searchArgs argument. 

Hope that helps you!

Regards,

Santosh


Santosh Sai

AEM BlogsLinkedIn


Avatar

Correct answer by
Level 2

Hi @SantoshSai ,

 

thank you, I've tried this way extending the ProductList model successfully but the query is executed inside the SearchResultsServiceImpl class that is in fact a Service, so I would have to override the whole "performQuery" method.

Instead I've found this project https://github.com/adobe/graphql-java-generator with which I can define a precise magento schema to generate custom Java classes. With those classes I can rewrite the ProductList model generating my custom graphql query. I know it is not an extension of the existing models and it will require some time initially, but maybe is a solution I can re-use with other custom components in the same project.

 

Thank you anyway and I hope I've chosen a right solution for this problem.