Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 3

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.

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@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

    }

  }

}

 

 

 

View solution in original post

2 Replies

Avatar

Correct answer by
Employee Advisor

@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

    }

  }

}

 

 

 

Avatar

Level 4

@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?