3

I am having following content tree:

enter image description here

In GraphQL (GQL), if I am rendering Product-1 (current context item) and wanted to access all the categories where Product-1 referenced in the multi-list field of Category items. I am not able to find the way to access the Categories where Product referenced in GraphQL (GQL). Can someone please guide/suggest the way to achieve above scenario using GQL?

2 Answers 2

1

You can do it with the search, there is a little caveats with searching on an GUID, you need to use a lowercase GUID without brackets and dashes.

Example:

{
  search( 
   fieldsEqual:[{name:"Products",value:"8826e32b70ac42aa923aff0a3a313f34"}] 
  rootItem:"/sitecore/content/MyApp/Categories"
       ) {
   results {
     items {
       item {
         id
         name
         path
       }
     }
     totalCount
   }
  }
}

Or a sample query with also the Products include. In This Example the value 8826e32b70ac42aa923aff0a3a313f34 is the GUID of your product and should be in this lowercase format.

{
  search( 
   fieldsEqual:[{name:"Products",value:"8826e32b70ac42aa923aff0a3a313f34"}] 
  rootItem:"/sitecore/content/Categories"
       ) {
   results {
     items {
       item {
         id
         name
         path
         products: field(name  : "Products")  {
          value
          ...Products
         }     
       }
     }
     totalCount
   }
  }
}

fragment Products on MultilistField {
  tags:targetItems {
  name
  id
  }
}

Also you can get the data without the search but currently not sure how to filter this, but with the following query you get al the data you need, but it contains all Categories, so to much data, but you can filter it later (outside GraphQL). at least a good starting point.

query MyDemoQuery( $contextItem: String!) 
{
  contextItem: item(path: $contextItem) {
    id
  }
  categories: item(path:"/sitecore/content/MyApp/Categories") {
    id
    children{
      id
      products:field(name  : "Products") {
      ...Products
      }
    } 
  }
}


fragment Products on MultilistField {
  tags: targetItems {
   id
   name
  }
}
3
  • Thanks for the information. In my case I have to use current context id (rendering added in product item, so product id would be used for filter) at run time as a filter. Can you please let me know how in search query, Id (product item id) of current item can be used to filter the categories in integrated GQL? The other query option (context item) already tried and not able to find the way to filter categories inside the GQL. Filter at FE not seems to be feasible.
    – Amit Kumar
    Commented May 28, 2019 at 15:40
  • I think it is a lot easier with a custom Rendering Contents Resolver C# instead of GraphQL, probably you needed to extend GraphQL.. Or create a categories searchindex with fields to search on,I think the paths of the products should be work, for $contextItem Commented May 28, 2019 at 19:54
  • I am also thinking to use custom resolver for this use case instead of GQL.
    – Amit Kumar
    Commented May 29, 2019 at 1:08
0

This is not possible out of the box, but it should be possible to implement a new query based on the existing fieldsEqual functionality.

fieldsEqual tests for exact equality.

foreach (Dictionary<string, object> dictionary in dictionaries)
{
    string name = dictionary["name"].ToString();
    string value = dictionary["value"].ToString();
    queryable = queryable.Where<ContentSearchResult>((Expression<Func<ContentSearchResult, bool>>) (result => result[name].Equals(value)));
}

But you'd want something like:

GraphQL query:

search(
  rootItem: "path/to/categories",
  fieldsContains: [{
    name: "productsfieldname",
    value: "{guid-of-product}"
  }]
)

Relevant query section in C# code:

foreach (Dictionary<string, object> dictionary in dictionaries)
{
    string name = dictionary["name"].ToString();
    string value = dictionary["value"].ToString();
    queryable = queryable.Where<ContentSearchResult>((Expression<Func<ContentSearchResult, bool>>) (result => result[name].Contains(value)));
}
2
  • Thanks for the information. In case of GQL, I have to take Id of current item where rendering added then need to filter categories (path or I'd of categories folder required) on the basis of product id. Can we use search query in integrated mode? If yes, can you please share the sample, if possible. AFAIK the custom c# code can be used in custom resolver. is my understanding correct?
    – Amit Kumar
    Commented May 28, 2019 at 14:48
  • I have written custom resolver and in which i am accessing the children of current item and returning children. In the ReactJS component i am not getting the items via sitecoreContext.route.fields. I checked the JSON response and found that item present sitecoreContext.route.placeholders.myplaceholder[0].items. Currently only one component present then i can access via index value. Can't i get the json data in ReactJS component via sitecoreContext.route.fields?sitecorechat.slack.com/files/U8MTHHT1A/FJRL0B76W/…
    – Amit Kumar
    Commented May 29, 2019 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.