Is it possible to add variation filter in nested content fragment model within a content fragment in GraphQL? | Community
Skip to main content
Level 4
December 13, 2022

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

  • December 13, 2022
  • 1 reply
  • 1535 views

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 } } } }
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

krati_garg
Adobe Employee
Adobe Employee
December 13, 2022

@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

 

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
      }
    }
  }
}
Level 4
December 13, 2022

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?

Level 4
December 15, 2022

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
      }

 

Query:

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

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

}
}
}


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