Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Is it possible to add variation filter in nested content fragment model within a content fragment in GraphQL?

Avatar

Level 5

I have a content fragment which includes a nested content fragment and I want to apply variation only to the nested CF results. Is it possible to do so via GQL?

 

Example:

 

query($variation:String!){
  articleByPath(_path: "/content/dam/wknd-shared/en/magazine/alaska-adventure/alaskan-adventures") {
    item {
      authorFragment(variation: $variation) {
        _path
        _variation
        firstName
        lastName
        birthDay
      }
      main {
        html
        markdown
        plaintext
        json
      }
    }
  }
}
6 Replies

Avatar

Employee Advisor

@spidey1405 

I believe there is no single query, which could get a variation of a nested fragments, but I could come up with below set of queries to achieve this:

With below query, you can get the variation name of a nested fragment:

{
articleByPath (_path: "/content/dam/wknd/en/magazine/skitouring/skitouring") {
item {
_path
author
referencearticle {
_path
author
_variations
}

}
}
}

 

Here referencearticle is a nested fragment, and variation1 is one of its variation

krati_garg_0-1670921453892.png

 

You can then fetch content out of variation1 of the nested fragment:

 

{
  articleByPath(_path: "/content/dam/wknd-shared/en/magazine/alaska-adventure/alaskan-adventures", variation: "variation1") {
    item {
      authorFragment {
        _path
        _variation
        firstName
        lastName
        birthDay
      }
      main {
        html
        markdown
        plaintext
        json
      }
    }
  }
}

Avatar

Level 5

Hi Krati, the query

{
  articleByPath(_path: "/content/dam/wknd-shared/en/magazine/alaska-adventure/alaskan-adventures", variation: "variation1") {
    item {
      authorFragment {
        _path
        _variation
        firstName
        lastName
        birthDay
      }
      main {
        html
        markdown
        plaintext
        json
      }
    }
  }
}

would return me article with the variation "variation1" however I am interested in something like this:

 

query($variation:String!){
  articleByPath(_path: "/content/dam/wknd-shared/en/magazine/alaska-adventure/alaskan-adventures") {
    item {
      authorFragment(variation: $variation) {
        _path
        _variation
        firstName
        lastName
        birthDay
      }
      main {
        html
        markdown
        plaintext
        json
      }
    }
  }
}

Is this possible?

Avatar

Employee Advisor

@spidey1405 Not exactly the above query, but below one worked for me:

krati_garg_0-1670945419413.png

query($variation:String!){
articleList( variation: $variation) {
items {
_path
referencearticle {
_path
_variations

}
main {
html
markdown
plaintext
json
}
}
}
}

Avatar

Level 5

This one I am aware of but its not solving the use case. I wanted to add variation to the nested model

Avatar

Employee Advisor

I twisted the query a bit and found that variable argument you are passing to the main fragment, is indeed passing to nested fragment. Please check below, main Content Fragment does not have any variations, while nested fragment has 2 variations, when I pass "test" as parameter, it picks "test" variation of nested fragment. 

You just need to place below code fragment to the item, where you want to apply the variable parameter:

 main {
        html
        markdown
        plaintext
        json
      }

 

krati_garg_0-1671048572210.png

Query:

query($variation:String!){
articleList(variation: $variation){

items {
_path
_variations
referencearticle{
_path
_variations
main {
html
markdown
plaintext
json
}
}

}
}
}

Avatar

Level 5

Thank you very much for the reply @krati_garg 

 

However this is not exactly desired. What I mean to say is that it should apply the variation only in the nested part and not in the whole articleList as you showed.

 

It should apply variation in the referencearticle but should return the master result of articleList