How to extend graphql query search arguments | Community
Skip to main content
Guglielmo89
Level 2
June 16, 2022
Solved

How to extend graphql query search arguments

  • June 16, 2022
  • 1 reply
  • 1373 views

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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Guglielmo89

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.

1 reply

SantoshSai
Community Advisor
Community Advisor
June 16, 2022

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
Guglielmo89
Guglielmo89AuthorAccepted solution
Level 2
June 17, 2022

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.