How to use GraphQL query filter with path and array variable for content fragments? | Community
Skip to main content
Level 2
March 6, 2023
Solved

How to use GraphQL query filter with path and array variable for content fragments?

  • March 6, 2023
  • 1 reply
  • 4886 views

I'm currently using AEM 6.5 and trying to implement GraphQL and Persisted Query API to retrieve content fragment data. Say my content fragment model's schema type looks like this:

type Message {
_path: ID,
id: String,
body: MultiFormatString

Each content fragment has a unique id that is required.

The content fragments are stored under /content/dam/mysite/messages/{pageid}/ folder.

I want to create persisted queries that allow me to retrieve content fragments based on the page id and a list of ids. Specifically, I want to achieve the following:

  1. Send a pageid as a parameter to retrieve all message content fragments under the specified page folder. 
  2. Send a list containing multiple ids to retrieve a list of message content fragments (like using a WHERE id in $ids condition in SQL).

I'm wondering if it's possible to achieve my goals using only GraphQL and Persisted Query API, or if I need to develop a custom servlet to implement more complicated search logic.

 

--------------------------
Additionally: 

For the 1st requirement, I want to use a filter like this, but AEM does not recognize the + sign in the filter, and the _path's filter does not support regex (Operators). 

{
_path: {
_expression: {
value: "/content/dam/mysite/messages/" + $pageid,
_operator: STARTS_WITH
}
}
}

For the 2nd requirement, I have also checked the AEM documentation, but it doesn't mention anything about accepting an array as a parameter or using an array to filter string data.

 

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

@wei_lyu 

 + as a operator cannot be used in a GrapqhQL query, especially for _path, since its data type is not String rather ID data type, hence String operations cannot be performed for _path/

 

For Requirement 1: You might have to change your solution approach, where in you can add a unique id to each folder, which can then be filtered using String Type.

 

For Requirement 2: Please find the sample code attached

 

query {

  cityList(filter: {

    categories: {

      _expressions: [

        {

          values: [

            "city:beach",

            "city:na"

          ]

        }

      ]

    }

  }) {

    items {

      name

      population

      country

      categories

    }

  }

}

 

 

 

1 reply

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

@wei_lyu 

 + as a operator cannot be used in a GrapqhQL query, especially for _path, since its data type is not String rather ID data type, hence String operations cannot be performed for _path/

 

For Requirement 1: You might have to change your solution approach, where in you can add a unique id to each folder, which can then be filtered using String Type.

 

For Requirement 2: Please find the sample code attached

 

query {

  cityList(filter: {

    categories: {

      _expressions: [

        {

          values: [

            "city:beach",

            "city:na"

          ]

        }

      ]

    }

  }) {

    items {

      name

      population

      country

      categories

    }

  }

}

 

 

 

Level 3
July 11, 2023

@krati_garg Could you please elaborate the solution for Requirement 1? How do I add the unique ID? Do you mean to add it as a field in the model and filter by that?