1

I am trying to get all the items in the certain folder based on 2 templates (Article, News) with the following GraphQL query. However i am facing to add the second template with "or" condition in the query and the query is not executed.

I do find few basic queries in the sitecore documentation.

Is there any good documentation and query examples? i would like to filter and sort in GraphQL (like sort by name, filter by some dates with in the sitecore items).

Also, my understanding is that when i do the "search" in the query, it returns the result from Solr index not from the sitecore DB's. is this correct understanding?

{
  search(  
  rootItem:"/sitecore/content/"        
  fieldsEqual:[
      {name: "_templatename", value:"Article"},
      or:{name: "_templatename", value:"News"}  
  ]
    )
  {
  results{
      totalCount
      Articles:items{              
          id
          name
          path                                    
      }
  }
 }
}

3 Answers 3

2

Hi in XM cloud both Experience Edge GraphQL and Preview Environment GraphQL now supports Searching, filtering using(multilist,treelist,single line text).

Also it supports date field filteration with GTE,LTE,etc in beta mode for few customers and only available in Experience EDGE) Also it supports the multiple AND and OR condition without need of any external indexing

Sample query

query {
  details: search(
    where: {
      AND: [
        { name: "_hasLayout", value: "true" }
        {
          name: "_path"
          value: "{969978C7-AA12-4A43-A959-352774E14206}"
          operator: CONTAINS
        }
        {
          name: "_templates"
          value: "{19885DFD-B59A-442A-A9C6-68D8587D78E1}"
          operator: CONTAINS
        }
        {
          OR: [
            {
              name: "Categories"
              value: "{E1DE8D2F-DFE7-4267-BA01-4D6050D89036}"
              operator: CONTAINS
            }
            {
              name: "Categories"
              value: "{DB178660-8E33-4328-85AD-54495F8BED9B}"
              operator: CONTAINS
            }
          ]
        }
        {
          OR: [
            {
              name: "Categories"
              value: "{29313328-F6E5-4D37-947E-0F5EF598F68D}"
              operator: CONTAINS
            }
            {
              name: "Categories"
              value: "{9D76FDB8-6833-4B33-85B4-9A9869D94B2A}"
              operator: CONTAINS
            }
            {
              name: "Categories"
              value: "{98AE98D1-11ED-426D-9302-9BB513696F87}"
              operator: CONTAINS
            }
            {
              name: "Categories"
              value: "{8AE738AC-24B5-4311-895B-F8E55A83A852}"
              operator: CONTAINS
            }
            {
              name: "Categories"
              value: "{73B3BB3D-4E11-46AD-8854-3E9CA2246220}"
              operator: CONTAINS
            }
          ]
        }
      ]
    }
    # defaults to 10
    first: 10

    orderBy: { name: "Heading", direction: ASC }
  ) {
    total
    pageInfo {
      endCursor
      hasNext
    }
    results {
      Id: id
      Pageurl: url {
        url
      }
    }
  }
}
0

Search() in Sitecore GraphQL uses Sitecore Content Search API. After you perform search you could check Search.Log in your instance to see what Solr query was generated. For your requirements below example should get you going:

{
  search(
    where: {
      operator: CONTAINS
      name: "_path"
      value: "{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}"
      AND: [
        {
          OR: [
            {
              name: "_templates"
              value: "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
            }
            {
              name: "_templates"
              value: "{6AC61576-DC57-410B-AB51-858E01B57753}"
            }
          ]
        }
      ]
    }
    orderBy: { name: "_name", direction: ASC }
  ) {
    results {
      name
      path
    }
  }
} 

Using Sitecore GraphQL UI should help you with showing available fields in GraphQL Schema /sitecore/api/graph/items/master/ui or for edge /sitecore/api/graph/edge/ui. Edge examples provided by Sitecore https://doc.sitecore.com/xp/en/developers/hd/210/sitecore-headless-development/query-examples.html. Also have a look at following related question https://sitecore.stackexchange.com/a/29750/7309.

3
  • Thanks Kamil. I have Checked the above query. It's for Experience Edge isn't it? GraphQL end point and Edge end point both same? in the GraphQL, when i place this query with GraphQL endpoint, it's erroring out. But when i use the edge point, it's NOT erroring out. But for some reason it's returning empty results. (I have provided the correct path, templates GUID with - and without - and small case also.
    – Jey
    Commented Mar 27, 2023 at 22:06
  • Have you passed sc_apikey header or query string?
    – Kamil C
    Commented Mar 28, 2023 at 6:39
  • Thanks Kamil. I has some syntax error. It works now. By default, query is going to the master index. How to send the query to any custom index? GraphQL API has the property to pass "index". But edge i think it doesn't have that option
    – Jey
    Commented Mar 28, 2023 at 15:33
0

You can extend the built-in Content Search API query and adds the ability to pass in conditions that can be OR'ed or AND'ed when the query executes. Conditions can also be grouped.

@Adam Lamarre tried and explain a similar implementation here. You can try it out and customize it as per your need. It gives you some idea to move forward in a direction at least.

For sort and filter by dates, The OOTB parameters allow for very powerful and fast queries for data but are limiting by a key factor. It does not support sorting, so you cannot control the order in which your results are returned. You need to extend SearchQuery to support date sorting. You can try out this approach here.

Hope it helps!

1
  • Thanks Sumit. So, it looks the GraphQL API OOTB does not support the OR condition. But the edge endpoing supports as Kamil's answer below.
    – Jey
    Commented Mar 28, 2023 at 15:21

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.