GraphQL Folder Ordering | Community
Skip to main content
Level 2
February 9, 2023
Solved

GraphQL Folder Ordering

  • February 9, 2023
  • 3 replies
  • 2310 views

Hi All,

 

We are looking at moving from the assets API to using GraphQL. One of the things we have noticed is that we can't get the ordering of content fragments within a folder back from the GraphQL.

 

Ordering is important to us. Does anyone have any advice on how to pull the ordering of Content Fragments back through GraphQL or in another simple API call without having to traverse the structure to build it up?

 

Thanks in advance.

 

 

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 krati_garg

@aem_party You might have to rethink over strategy. I can still see following ways:

A. If they belong to same CFM, then consider 2nd option in my above comment -  Add a field in the CFM, which would increment or indicate the order of its creation in a folder. And base your Sorting Order in Graphql Query with that Field.

B. Query Hybrid, get all the CFs in a folder via Asset HTTP API, to maintain the natural order. Iterate over the response and trigger Graphql Query with CF Path fetched from Asset HTTP API. 

 

Please note for Option B, you might have to scalability and performance issues, for increasing number of CFs in a folder.

3 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
February 9, 2023
AEM_PARTYAuthor
Level 2
February 10, 2023

Thanks for that but what I need to know is the order of the Content Fragments in the folder.

 

I can't see a way to get this out of GraphQL whereas the assets API gives you the Content Fragments in order.

Saravanan_Dharmaraj
Community Advisor
Community Advisor
February 10, 2023

When you query(GraphQL API) a particular folder without any filtering/sorting in the query, does it give the results item in random fashion than what is order stored in JCR? 

krati_garg
Adobe Employee
Adobe Employee
February 10, 2023

Hi @aem_party 

Please check the below link which says that by default sorting criteria is repository path:

By default it uses the repository path of each item of the result set to make sure the order is always the same

 

https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/headless/graphql-api/content-fragments.html#sorting:~:text=By%20default%20it%20uses%20the%20repository%20path%20of%20each%20item%20of%20the%20result%20set%20to%20make%20sure%20the%20order%20is%20always%20the%20same.

Hence, if the ask is to get the CFs in order they were added to a folder, I can think of two ways:

1. Either follow the naming of CFs as such that they are fetched as per their naming order. In this case you might not have to define sorting order explicitly
2. Add a field in the CFM, which would increment or indicate the order of its creation in a folder. And base your Sorting Order in Graphql Query with that Field

 

 

Hope this helps. 

AEM_PARTYAuthor
Level 2
February 13, 2023

Thanks for the pointers but sadly these don't work.

 

For example I have a folder:

 

  • Folder called Shop
    • Content Fragment: Welcome
    • Content Fragment: Product Overviews
    • Content Fragment: Contact

 

You can see I can't infer an order form the names. Also, the order the CFs are entered in is not useful either as I may create another Content Fragment say, "Environment" and I want that to slot in above "Contact".

 

krati_garg
Adobe Employee
krati_gargAdobe EmployeeAccepted solution
Adobe Employee
February 13, 2023

@aem_party You might have to rethink over strategy. I can still see following ways:

A. If they belong to same CFM, then consider 2nd option in my above comment -  Add a field in the CFM, which would increment or indicate the order of its creation in a folder. And base your Sorting Order in Graphql Query with that Field.

B. Query Hybrid, get all the CFs in a folder via Asset HTTP API, to maintain the natural order. Iterate over the response and trigger Graphql Query with CF Path fetched from Asset HTTP API. 

 

Please note for Option B, you might have to scalability and performance issues, for increasing number of CFs in a folder.

bruno-bradach
Level 2
June 15, 2023

What you can do is to create a "root" CF Model that includes a fragment reference multifield. There, you can add references to all fragments you want to list, and the multifield allows you to reorder them manually.

 

Of course this is a pain if you handle many CFs, but might solve your problem if you handle a few CFs.

 

It's a shame GraphQL doesn't follow the order of the CFs in the folder which can be reordered from the list view.