My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and index you

Sitecore Experience Edge GraphQL Queries

The Headless development in Sitecore is powered by a layer of services, API endpoints, and rendering SDKs:
Services and API endpoints

Rendering SDKs

In this article, we will explore about Sitecore scalable API layer called Sitecore Experience Edge which provides you a Sitecore-hosted GraphQL (GQL) API. With these Edge GraphQL (GQL) endpoint you can build your solution in any language and pull the required content with the help of GraphQL (GQL).

The major GraphQL APIs of Sitecore Experience Edge are:
  • Preview API
  • Delivery API
You can check more details about these APIs at Type of Sitecore Experience Edge API.

GraphQL is a query language for these APIs that gives you a flexible way to query the data. It also provides the flexibility to define API endpoints with custom url's. These endpoints host the schema, which is a strongly typed graph definition where endpoints understand the GraphQL language.

I have explained in detail about Sitecore GraphQL and Sitecore Experience Edge and in my previous blog posts:
This article will provide the details about Sitecore GraphQL Edge queries usage and how it will be useful in creating Sitecore projects especially Sitecore JSS projects. The topics covered under this Sitecore GraphQL Edge queries article are:


Overview of Sitecore GraphQL (GQL)
To get more details about Sitecore GraphQL, you can check my article Sitecore GraphQL Queries:

Sitecore GraphQL Architecture

How to test Sitecore Experience Edge GraphQL (GQL) queries?
You can use interactive browser-based GraphQL Integrated Development Environment (IDE) which based on the GraphQL Playground to validate your queries during the development phase with production schema, and without pushing to PROD environment.

To get more details about Sitecore Edge GraphQL Integrated Development Environment (IDE), you can check my previous article Quickstart guide - All about Sitecore Experience Edge.

How to access Sitecore XM / Sitecore XM Cloud Edge with Sitecore Edge IDE and APIs?
We can access the Edge GraphQL Integrated Development Environment (IDE) with Preview API and Delivery API. The IDEs/APIs URLs are different for both Sitecore XM and Sitecore XM Cloud.
  • Sitecore Experience Manager (XM)
    • Sitecore Edge Preview IDE:
      You have to use the preview IDE URL as https://[WEBSITE HOST ADDRESS]/sitecore/api/graph/edge/ui and preview API URL as https://[WEBSITE HOST ADDRESS]/sitecore/api/graph/edge, and need to replace [WEBSITE HOST ADDRESS] with your Sitecore CM instance host address

      To access the conentent:
      You have to pass the API key and check the details at Sitecore Experience Manager (XM) Preview API Key
      Once you have the API key then you have to pass the API key to the HTTP header of the API request
      If you are accessing Edge GQL IDE in Sitecore Experience Platform (SXP) then may be sometimes you have to set the IDE Request Credentials setting and need to use the API Key present at /sitecore/system/Settings/Services/API Keys
    • Sitecore Edge Delivery IDE:
      You have to use the delivery IDE URL as https://edge.sitecorecloud.io/api/graphql/ide and delivery API URL as https://edge.sitecorecloud.io/api/graphql/v1:

      To access the conentent:
      You have to pass the API key, if you are using Sitecore Demo Portal (this portal are NOT XM Cloud based. They are container environments hosted on AKS) then you can check steps at Sitecore Demo Portal Edge GraphQL API Key to get the API key and if you are using Sitecore SXP then need to use the API Key present at /sitecore/system/Settings/Services/API Keys but you can't use GQL IDE for Sitecore SXP Content Delivery instance because its disabled.
      Once you have the API key then you have to pass the API key to the HTTP header of the API request.
  • Sitecore Experience Manager (XM) Cloud
    • Sitecore Edge Preview IDE:
      You have to use the preview IDE URL as https://[WEBSITE HOST ADDRESS]/sitecore/api/graph/edge/ide and preview API URL as https://[WEBSITE HOST ADDRESS]/sitecore/api/graph/edge, and need to replace [WEBSITE HOST ADDRESS] with your Sitecore XM Cloud CM instance host address

      To access the conentent:
      You have to pass the API key and check the details at Sitecore Experience Manager (XM) Cloud Preview API Key
      Once you have the API key then you have to pass the API key to the HTTP header of the API request
    • Sitecore Edge Delivery IDE:
      You have to use the delivery IDE URL as https://edge.sitecorecloud.io/api/graphql/ide and delivery API URL as https://edge.sitecorecloud.io/api/graphql/v1:

      To access the conentent:
      You have to pass the API key, if you are using Sitecore XM Cloud Portal then you can check steps at Sitecore XM Cloud Edge GraphQL Delivery API Key to get the API key.
      Once you have the API key then you have to pass the API key to the HTTP header of the API request.
Sitecore Edge GraphQL (GQL) Queries Usage:
For this article, I am using following Sitecore Content Trees:

Sitecore Demo Portal Content Tree:

Sitecore XM Portal Content Tree:

The examples of Sitecore Edge GraphQL queries are:
  • Sitecore Edge GraphQL - Get an item by path and show field id and name

    query GQLQuerySamples( $contextItem: String!) {
                   #----Get an item by path and show field id and name:: Start ::------
                    GQLType1: item(path: $contextItem, language:"en"){
                    id,
                    name
                  }
                  #----Get an item by path and show field id and name:: End ::------
                }
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                # {"contextItem" :"/sitecore/content/Website/Website1/Home"}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL - Return the custom name of field

    query GQLQuerySamples( $contextItem: String!) {
                  #----Custom name for a field, test contain the id value:: Start ::------
                      GQLType2: item(path: $contextItem,, language:"en") {
                        id
                        name
                        ItemId: id
                      }
                    #----Custom name for a field, test contain the id value:: End ::------
                  }
    
                  # in the above query, mind the ",," it will work.
    
                  #--- Sitecore Edge Graphql Query Variables ------::Start::------
                     #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                          # and place in the QUERY VARIABLES section
    
                          # {"contextItem" :"/sitecore/content/Website/Website1/Home"}
                  #--- Sitecore Edge Graphql Query Variables ------::End::------
                  

  • Sitecore Edge GraphQL - Return the name and value from all fields

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                  #----Return the name and value from all fields:: Start ::------
                    GQLType3: item(path: $contextItem,language:$language) {
                        fields  {
                        name
                      value
                     }
                    }  
                  #----Return the name and value from all fields:: End ::------  
                }
    
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                        # {
                        #  "contextItem" :"/sitecore/content/Website/Website1/Home",
                        #  "language" :"en"
                        #}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

    With the help of this you can get the list of available fields. 

  • Sitecore Edge GraphQL - Return a specific field with Alias

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                        #----Return a specific field, in this case field "Title" with Alias:: Start ::------  
    
                        GQLType4: item(path: $contextItem,language:$language) {
                            PageTitle:field(name  : "Title") {
                           MyPageTitle:value
                          }
    
                        }  
                      #----Return a specific field, in this case field "Title" with Alias:: End ::------  
                    }
    
    
                    #--- Sitecore Edge Graphql Query Variables ------::Start::------
                       #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                            # and place in the QUERY VARIABLES section
    
                            # {
                            #  "contextItem" :"/sitecore/content/Website/Website1/Home",
                            #  "language" :"en"
                            #}
                    #--- Sitecore Edge Graphql Query Variables ------::End::------
                    

  • Sitecore Edge GraphQL - Get Child Items using GraphQL

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                  #----Get a children: You can access only common values :: Start ::------
                    #child specific values or get all field values as per Example# 3
                    GQLType5: item(path: $contextItem,language:$language) {
                      id
                      name
                         children
                      {
                                results
                        {
                            id
                          name
                                    fields  {
                                name
                                value
                                }          
                        }
    
                      }
                    }  
                  #----Get a children: You can access only common values :: End ::------ 
                }
    
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                        # {
                        #  "contextItem" :"/sitecore/content/Website/Website1/Home",
                        #  "language" :"en"
                        #}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

    In the above example, we are trying to get the child items of HOME node.

  • Sitecore Edge GraphQL - Get a typed safe data using GraphQL

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                    #----Get a typed safe data on the basis of template :: Start ::------
                      GQLType6: item(path: $contextItem,language:$language) {
                           children
                        {
                                  results
                          {
                            ... on Speaker
                            {
                              id
                              name
                              sponsors {value}
                              vendors {value}
                              sessions {value}
                              description {value}
                              role {value}
                              picture {value}
                            }
    
                          }
    
                        }
                      }
                    #----Get a typed safe data on the basis of template :: End ::------ 
                  }
    
    
                  #--- Sitecore Edge Graphql Query Variables ------::Start::------
                     #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                          # and place in the QUERY VARIABLES section
    
                          # {
                          #  "contextItem" :"/sitecore/content/EdgeWebsite/home/speakers",
                          #  "language" :"en"
                          #}
                  #--- Sitecore Edge Graphql Query Variables ------::End::------
                  

    If you have multiple items based on different templates then it will return result based on typed data if present (no error message) otherwise not, and you can combine multiple templates in one query.

  • Sitecore Edge GraphQL - Get specific Child Items using GraphQL and alias

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                  #----Get only specific children on the basis of Template :: Start ::------
                    GQLType7: item(path: $contextItem,language:$language) {
                     #-- Get only  Webpage
                         children (includeTemplateIDs: ["6DB09CEF64A852A3A98038408109C4D7"])
                      {
                                results
                        {
                          template{id
                          name
                          }
                          name
                          displayName
    
                          ... on Webpage
                          {
                                pageTitle {value}
                          }
    
                        }
    
                      }
                    }
                 #----Get only specific children on the basis of Template :: End ::------ 
                }
    
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                        # {
                        #  "contextItem" :"/sitecore/content/EdgeWebsite/home",
                        #  "language" :"en"
                        #}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL - Get the values of Tree-list or Multi-list field type

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                  #----Get value of multi-list field from item :: Start ::------
                    GQLType8: item(path: $contextItem,language:$language) {
                                ... on Speaker
                        {
                          id
                          name
                          sessions
                          {
                            SessionList:targetItems
                            {
                                            ... on Session
                             {
                                SessionId: id
                                SessionName: name
                                SessionPageTitle: pageTitle {value}
                                description {
                                  id
                                  value
                                  jsonValue
                                  definition {type}
                                }
                            }
                            }
    
                          }
    
                        }
                    }  
                  #----Get value of multi-list field from item :: End ::------
                }
    
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                        # {
                        #  "contextItem" :"/sitecore/content/EdgeWebsite/home/speakers/Alex Mena",
                        #  "language" :"en"
                        #}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL - Get the values of Treelist or Multilist field type from child item of current context

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                #----Get children of multi-list field from child item of current context :: Start ::------
                    GQLType9: item(path: $contextItem,language:$language) {
                                ... on Speaker
                        {
                          id
                          name
                          sessions
                          {
                            SessionList:targetItems
                            {
                                            ... on Session
                               {
                                  SessionId: id
                                  SessionName: name
                                  SessionPageTitle: pageTitle {value}
                                  # using Parent item to get its child
                                  parent 
                                   {
                                     id
                                     path
                                     # Get only some child items, if many different template item present
                                     children (includeTemplateIDs: ["{D8AA73E1-C275-4EEE-BB9D-333925E51952}"])
                                      {
                                        results
                                         {
                                           id
                                           name
                                           path
                                         }
                                      }
                                   }
                               }
                            }
    
                          }
    
                        }
                    }  
                  #----Get children of multi-list field from child item of current context :: End ::------
                }
    
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                        # {
                        #  "contextItem" :"/sitecore/content/EdgeWebsite/home/speakers/Alex Mena",
                        #  "language" :"en"
                        #}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL - Get the template field details with values of current item

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                    #----Get the template field details with values of current item :: Start ::------
    
                    GQLType10: item(path: $contextItem,language:$language) {
                                ... on Speaker
                        {
                          id
                          name
                                     __typename
                          templateName:template
                           {
                            id
                            name
                          }
                          # Fields  present on specific item
                          ... on Item 
                              {
                                fields {
                                  id
                                  name
                                  value
                                }
                              }          
                        }
                    }  
                 #----Get the template field details with values of current item :: End ::------ 
                }
    
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                        # {
                        #  "contextItem" :"/sitecore/content/EdgeWebsite/home/speakers/Alex Mena",
                        #  "language" :"en"
                        #}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL - Get the field values using Aliases

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                #----Get the field values using Aliases :: Start ::------
                  GQLType11: item(path: $contextItem,language:$language) {
                              ... on Speaker
                      {
                        id
                        SpeakerName: name
                        # You can add aliases to field attributes also
                                  role  {Speaker_Role:value}      
                      }
                  }  
              #----Get the field values using Aliases :: End ::------ 
              }
    
    
              #--- Sitecore Edge Graphql Query Variables ------::Start::------
                 #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                      # and place in the QUERY VARIABLES section
    
                      # {
                      #  "contextItem" :"/sitecore/content/EdgeWebsite/home/speakers/Alex Mena",
                      #  "language" :"en"
                      #}
              #--- Sitecore Edge Graphql Query Variables ------::End::------
              

  • Sitecore Edge GraphQL - Get repeated fields using Fragments

    query GQLQuerySamples( $contextItem: String!, $language: String!) {
                #---------Use this part to test Fragment Query Example-----------::Start::------------
                   GQLType12: item(path: $contextItem,language:$language) {
                                ... on Speaker
                        {
                          id
                          name
                          sessions
                          {
                           ... Sessions
                          }
    
                        }
                    }  
                }
    
    
                fragment Sessions on MultilistField {
                  SessionList:targetItems {
                  name
                  id
                         ... on Session
                    {
    
                      SessionId: id
                      SessionName: name
                      SessionPageTitle: pageTitle {value}
                      rooms
                      {
                        # Use of Fragment at child items
                        ... Rooms
                      }
                    }
                  }
                }
    
    
                fragment Rooms on MultilistField {
                  RoomList:targetItems {
                  name
                  id
                         ... on Room
                    {
    
                      RoomId: id
                      RoomName: name
                      RoomDescription: description {value}
                    }
                  }
                }
    
    
                  #---------Use this part to test Fragment Query Example-----------::End::------------
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
    
                        # {
                        #  "contextItem" :"/sitecore/content/EdgeWebsite/home/speakers/Alex Mena",
                        #  "language" :"en"
                        #}
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL Search - using Search query keyword

    #1. Sitecore Edge GraphQL Search Query _path usage
                # you will get all items under the /sitecore/content/EdgeWebsite/home/speakers {1F4B781B-F2A5-5647-99DF-C0C369162C4D}
                # and _path accept only GUID (with or without braces). 
                # It will return records from /page-components also
    
                query {
                  pageOne: search(
                     where: {
                       AND: [
                         {
                           name: "_path"
                           value: "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
                           operator: CONTAINS
                         }
    
                       ]
                     }
                     # defaults to 10
                     first: 10
                   ) {
                     total
                     pageInfo {
                       endCursor
                       hasNext
                     }
                     results {
                       url {
                         path
                       }
                     }
                   }
                 }
                

  • Sitecore Edge GraphQL Search - Apply filter not to return items from specific path

    #2. Sitecore Edge GraphQL Search - Apply filter not to return items from specific path
    # To filter not to return child items from particular path we can use 
    # NCONTAINS operator
    # If we want to exclude items under specific locations, e.g., need to hide items which are present under /sitecore/content/EdgeWebsite/home/speakers/Page Components:
     # GUID of /sitecore/content/EdgeWebsite/home/speakers/Page Components {81347F0C-A6F8-4A09-8D0A-0600485D5894}
    # It will not return records under the /Page Components 
    query SearchQuery (
      $pageSize: Int = 10
      $language: String = ""
      $rootpath: String = ""
    ) {
      pageOne: search(
         where: {
           AND: [
             {
               name: "_path"
               value:$rootpath
               operator: CONTAINS
             }
              {
              name: "_path"
              value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
              operator: NCONTAINS
            } 
            {
              name: "_language"
              value: $language
            } 
           
           ]
         }
         # defaults to 10
         first: $pageSize
       ) {
         total
         pageInfo {
           endCursor
           hasNext
         }
         results {
            id
          path
          
          fields{name
          __typename
          }
         }
       }
     }
     
    #--- Sitecore Edge Graphql Query Variables ------::Start::------
       #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
            # and place in the QUERY VARIABLES section
            #   {
            #    "pageSize": 30,
            #    "language": "en",
            #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
            #  }
            
    #--- Sitecore Edge Graphql Query Variables ------::End::------
    

    In the above search query, i am using fields attribute to get the list of FIELDS from Search Result Item. With this you can find list of available fields.

  • Sitecore Edge GraphQL Search – Parameter details

    #3. Sitecore Edge GraphQL Search - Apply filter not to return items from specific path
                # To filter not to return child items from particular path we can use 
                # NCONTAINS operator
                # If we want to exclude items under specific locations, e.g., need to hide items which are present under /sitecore/content/EdgeWebsite/home/speakers/Page Components:
                 # GUID of /sitecore/content/EdgeWebsite/home/speakers/Page Components {81347F0C-A6F8-4A09-8D0A-0600485D5894}
                # It will not return records under the /Page Components 
                query SearchQuery (
                  $pageSize: Int = 10
                  $language: String = ""
                  $rootpath: String = ""
                ) {
                  pageOne: search(
                     where: {
                       AND: [
                         {
                           name: "_path"
                           value:$rootpath
                           operator: CONTAINS
                         }
                          {
                          name: "_path"
                          value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
                          operator: NCONTAINS
                        } 
                        {
                          name: "_language"
                          value: $language
                        } 
    
                       ]
                     }
                     # defaults to 10
                     first: $pageSize
                   ) {
                     total
                     pageInfo {
                       endCursor
                       hasNext
                     }
                     results {
                        id
                      path
    
                      fields{name
                      __typename
                      }
                     }
                   }
                 }
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
                        #   {
                        #    "pageSize": 10,
                        #    "language": "en",
                        #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
                        #  }
    
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

    In the above search query, we are having following parameters:
    total: represent total number of records
    first: this repsresent the page size of the search results and you can change as per your need
    endCursor: Contains the encoded value of the cursor pointing to the last hit of a page
    hasNext: this represents that next page present or not

    Here total records are 21 and page size is 10 so total number of pages would be 3. If i will set the page size as 30 then hasNext should come as false, it means only ONE page.


  • Sitecore Edge GraphQL Search - Hide items based on specific template

    #4. Sitecore Edge GraphQL Search - Hide items based on specific template
              query SearchQuery (
                $pageSize: Int = 10
                $language: String = ""
                $rootpath: String = ""
              ) {
                pageOne: search(
                   where: {
                     AND: [
                       {
                         name: "_path"
                         value:$rootpath
                         operator: CONTAINS
                       }
                        {
                        name: "_path"
                        value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
                        operator: NCONTAINS
                      } 
                      {
                        # Item based on Folder template, e.g., Page Components
                        name: "_templates"
                        value: "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}"
                        operator: NCONTAINS
                      } 
                      {
                        name: "_language"
                        value: $language
                      } 
    
                     ]
                   }
                   # defaults to 10
                   first: $pageSize
                 ) {
                   total
                   pageInfo {
                     endCursor
                     hasNext
                   }
                   results {
                      id
                    path
    
                    fields{name
                    __typename
                    }
                   }
                 }
               }
    
              #--- Sitecore Edge Graphql Query Variables ------::Start::------
                 #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                      # and place in the QUERY VARIABLES section
                      #   {
                      #    "pageSize": 30,
                      #    "language": "en",
                      #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
                      #  }
    
              #--- Sitecore Edge Graphql Query Variables ------::End::------
              

  • Sitecore Edge GraphQL Search – Order By Field and Order by Direction

    #5. Sitecore Edge GraphQL Search – Order By Field and Order by Direction
                query SearchQuery (
                  $pageSize: Int = 10
                  $language: String = ""
                  $rootpath: String = ""
                  $orderByField: String!  
                  $orderDirection: OrderByDirection = ASC  
                ) {
                  pageOne: search(
                     where: {
                       AND: [
                         {
                           name: "_path"
                           value:$rootpath
                           operator: CONTAINS
                         }
                          {
                          name: "_path"
                          value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
                          operator: NCONTAINS
                        } 
                        {
                          # Item based on Folder template, e.g., Page Components
                          name: "_templates"
                          value: "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}"
                          operator: NCONTAINS
                        } 
                        {
                          name: "_language"
                          value: $language
                        } 
    
                       ]
                     }
                     # defaults to 10
                     first: $pageSize
                    orderBy: { name: $orderByField, direction: $orderDirection }
                   ) {
                     total
                     pageInfo {
                       endCursor
                       hasNext
                     }
                     results {
                        path
                        ... on Speaker
                          {
                            jobTitle {value}
                          }
                     }
                   }
                 }
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   # Root path is /sitecore/content/EdgeWebsite/home/speakers
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
                        #   {
                        #    "pageSize": 30,
                        #    "language": "en",
                        #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
                        #     "orderByField": "JobTitle", 
                        #     "orderDirection": "ASC"
                        #  }
    
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL Search – AND / OR Condition (ItemSearchPredicateInput) Usage

    #6. Sitecore Edge GraphQL Search – AND / OR Condition (ItemSearchPredicateInput) Usage
                # You can add AND / OR clause to search query with different field names
                # if you are using the CONTAINS clause then it will search the keyword in field content and return the results, and keyword case (UPPER or LOWER case) wouldn't affect the search results
    
                query SearchQuery (
                  $pageSize: Int = 10
                  $language: String = ""
                  $rootpath: String = ""
                  $orderByField: String!  
                  $orderDirection: OrderByDirection = ASC  
                  $searchKeyword: String = "" 
                  $valueAfter: String = "" 
                ) {
                  pageOne: search(
                     where: {
                       AND: [
                         {
                           name: "_path"
                           value:$rootpath
                           operator: CONTAINS
                         }
                          {
                          name: "_path"
                          value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
                          operator: NCONTAINS
                        } 
                        {
                          # Item based on Folder template, e.g., Page Components
                          name: "_templates"
                          value: "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}"
                          operator: NCONTAINS
                        } 
                        {
                          name: "_language"
                          value: $language
                        } 
                        {
                                    OR: [
                            {
                              name: "JobTitle"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            {
                              name: "pageTitle"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            {
                              name: "Description"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            ]          
                        }       
                       ]
                     }
                     # defaults to 10
                    first: $pageSize
                    after:$valueAfter
                    orderBy: { name: $orderByField, direction: $orderDirection }
                   ) {
                     total
                     pageInfo {
                       endCursor
                       hasNext
                     }
                     results {
                        path
                        ... on Speaker
                          {
                            jobTitle {value}
                            pageTitle{value}
                            description{value}
                          }
                     }
                   }
                 }
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   # Root path is /sitecore/content/EdgeWebsite/home/speakers
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
                        #   {
                        #    "searchKeyword": "alex", 
                        #    "pageSize": 30,
                        #    "language": "en",
                        #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
                        #     "orderByField": "JobTitle", 
                        #     "orderDirection": "ASC",
                        #     "valueAfter": ""
                        #  }
    
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

  • Sitecore Edge GraphQL Search – Pagination and first/after parameters

    #7. Sitecore Edge GraphQL Search – Pagination and first/after parameters
    # For pagination we need to use the parameter 'after' and we need to pass the value returned in "'pageInfo/endCursor'".
    #At FE app we can use "'pageInfo/hasNext'" to validate that next page present or not.
    
    #When you are executing initial request then 'after' parameter value would be empty and if query returned more than one page then we have to use the exact value returned in 'pageInfo/endCursor'" in 'after' parameter
    query SearchQuery (
      $pageSize: Int = 10
      $language: String = ""
      $rootpath: String = ""
      $orderByField: String!  
      $orderDirection: OrderByDirection = ASC  
      $searchKeyword: String = "" 
      $valueAfter: String = "" 
    ) {
      pageOne: search(
         where: {
           AND: [
             {
               name: "_path"
               value:$rootpath
               operator: CONTAINS
             }
              {
              name: "_path"
              value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
              operator: NCONTAINS
            } 
            {
              # Item based on Folder template, e.g., Page Components
              name: "_templates"
              value: "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}"
              operator: NCONTAINS
            } 
            {
              name: "_language"
              value: $language
            } 
            {
     					OR: [
                {
                  name: "JobTitle"
                  value: $searchKeyword
                  operator: CONTAINS
                }         
                {
                  name: "pageTitle"
                  value: $searchKeyword
                  operator: CONTAINS
                }         
                {
                  name: "Description"
                  value: $searchKeyword
                  operator: CONTAINS
                }         
          		]          
            }       
           ]
         }
         # defaults to 10
        first: $pageSize
        after:$valueAfter
        orderBy: { name: $orderByField, direction: $orderDirection }
       ) {
         total
         pageInfo {
           endCursor
           hasNext
         }
         results {
            path
            ... on Speaker
              {
                jobTitle {value}
                pageTitle{value}
                description{value}
              }
         }
       }
     }
     
    #--- Sitecore Edge Graphql Query Variables ------::Start::------
       # Root path is /sitecore/content/EdgeWebsite/home/speakers
       #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
            # and place in the QUERY VARIABLES section
            #   {
            #    "searchKeyword": "alex", 
            #    "pageSize": 30,
            #    "language": "en",
            #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
            #     "orderByField": "JobTitle", 
            #     "orderDirection": "ASC",
            #     "valueAfter": ""
            #  }
            
    #--- Sitecore Edge Graphql Query Variables ------::End::------
    

    For pagination we need to use the parameter 'after' and we need to pass the value returned in 'pageInfo/endCursor'.

    At FE app we can use 'pageInfo/hasNext' to validate that next page present or not.

    When you are executing initial request then 'after' parameter value would be empty and if query returned more than one page then we have to use the exact value returned in 'pageInfo/endCursor' in 'after' parameter.
    In the above query, i am trying to access the Page TWO content by passing the value of 'pageInfo/endCursor' received from INITIAL search request to 'after'.

    Now, if i will try to use the value of 'pageInfo/endCursor' received from above search results to 'after' parameter then i will get the EMPTY results:
  • Sitecore Edge GraphQL Search – Get all field name of the search result item

    #8. Sitecore Edge GraphQL Search – Get all field name of the search result item
    
                query SearchQuery (
                  $pageSize: Int = 10
                  $language: String = ""
                  $rootpath: String = ""
                  $orderByField: String!  
                  $orderDirection: OrderByDirection = ASC  
                  $searchKeyword: String = "" 
                  $valueAfter: String = "" 
                ) {
                  pageOne: search(
                     where: {
                       AND: [
                         {
                           name: "_path"
                           value:$rootpath
                           operator: CONTAINS
                         }
                          {
                          name: "_path"
                          value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
                          operator: NCONTAINS
                        } 
                        {
                          # Item based on Folder template, e.g., Page Components
                          name: "_templates"
                          value: "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}"
                          operator: NCONTAINS
                        } 
                        {
                          name: "_language"
                          value: $language
                        } 
                        {
                                    OR: [
                            {
                              name: "JobTitle"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            {
                              name: "pageTitle"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            {
                              name: "Description"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            ]          
                        }       
                       ]
                     }
                     # defaults to 10
                    first: $pageSize
                    after:$valueAfter
                    orderBy: { name: $orderByField, direction: $orderDirection }
                   ) {
                     total
                     pageInfo {
                       endCursor
                       hasNext
                     }
                     results {
                        template {
                          id
                          name
                        }
                        id
                        name
                        path
                        ... SearchResultsItems
                     }
                   }
                 }
    
                fragment SearchResultsItems on Item {
                     fields
                      {
                        name
                        value
                      }  
                }
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   # Root path is /sitecore/content/EdgeWebsite/home/speakers
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
                        #   {
                        #    "searchKeyword": "alex", 
                        #    "pageSize": 30,
                        #    "language": "en",
                        #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
                        #     "orderByField": "JobTitle", 
                        #     "orderDirection": "ASC",
                        #     "valueAfter": ""
                        #  }
    
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

    When you start writing the Sitecore Edge GraphQL Search Queries, you would not be aware about the name of the fields which you have to use, because you wouldn't be having access to Search Engine provider.

    In this case, the starting should be to get the list of all fields of all templates which you are going to use in your search queries.
  • Sitecore Edge GraphQL Search – Return a specific field with Alias

    #9. Sitecore Edge GraphQL Search – Return a specific field with Alias
                # You can provide user friendly name or alias to the field name as per your requirement.
    
                query SearchQuery (
                  $pageSize: Int = 10
                  $language: String = ""
                  $rootpath: String = ""
                  $orderByField: String!  
                  $orderDirection: OrderByDirection = ASC  
                  $searchKeyword: String = "" 
                  $valueAfter: String = "" 
                ) {
                  pageOne: search(
                     where: {
                       AND: [
                         {
                           name: "_path"
                           value:$rootpath
                           operator: CONTAINS
                         }
                          {
                          name: "_path"
                          value: "{81347F0C-A6F8-4A09-8D0A-0600485D5894}"
                          operator: NCONTAINS
                        } 
                        {
                          # Item based on Folder template, e.g., Page Components
                          name: "_templates"
                          value: "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}"
                          operator: NCONTAINS
                        } 
                        {
                          name: "_language"
                          value: $language
                        } 
                        {
                                    OR: [
                            {
                              name: "JobTitle"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            {
                              name: "pageTitle"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            {
                              name: "Description"
                              value: $searchKeyword
                              operator: CONTAINS
                            }         
                            ]          
                        }       
                       ]
                     }
                     # defaults to 10
                    first: $pageSize
                    after:$valueAfter
                    orderBy: { name: $orderByField, direction: $orderDirection }
                   ) {
                     total
                     pageInfo {
                       endCursor
                       hasNext
                     }
                     results {
                        template {
                          id
                          name
                        }
                        id
                        name
                        path
                        ... SearchResultsItems
                     }
                   }
                 }
    
                fragment SearchResultsItems on Item {
                     JobTitle: field(name  : "JobTitle") {
                           value
                          }  
                     fields
                      {
                        name
                        value
                      }  
                }
    
                #--- Sitecore Edge Graphql Query Variables ------::Start::------
                   # Root path is /sitecore/content/EdgeWebsite/home/speakers
                   #--- To use below Sitecore Edge Graphql Query Variables you have to uncomment 
                        # and place in the QUERY VARIABLES section
                        #   {
                        #    "searchKeyword": "alex", 
                        #    "pageSize": 30,
                        #    "language": "en",
                        #    "rootpath": "{1F4B781B-F2A5-5647-99DF-C0C369162C4D}"
                        #     "orderByField": "JobTitle", 
                        #     "orderDirection": "ASC",
                        #     "valueAfter": ""
                        #  }
    
                #--- Sitecore Edge Graphql Query Variables ------::End::------
                

Sitecore Edge GraphQL Query Cheat Sheet
Please find below list of Sitecore Edge GraphQL queries:

Important Notes
  • Sitecore Experience Manager (XM) Edge Preview API Key
    The API key for Sitecore CMS Present at /sitecore/system/Settings/Services/API Keys/ location, and if not present then you can create new API Key (using template /sitecore/templates/System/Services/API Key):
  • Sitecore Edge GraphQL Preview Request-Credentials
    If you are accessing the Sitecore Edge GraphQL IDE on Sitecore XP (SXP) then sometimes you will received Authentication failed related errors then you have to update the Request Credentials setting from omit to same-origin in IDE:
  • Sitecore Edge GraphQL IDE HTTP Headers
    If you are accessing the Sitecore Edge GraphQL IDE the you have to pass the API key to the HTTP header of API request, and you can directly set the API key in IDE:
  • Sitecore Experience Platform (SXP) Edge GraphQL API Key
    The API key for SXP Present at /sitecore/system/Settings/Services/API Keys/ location, and if not present then you can create new API Key (using template /sitecore/templates/System/Services/API Key):
    You have to publish this API Key before using it.
  • Sitecore Demo Portal Edge GraphQL API Key
    You can find API key for Sitecore Demo Portal at instance details section:
  • Sitecore XM Cloud Edge GraphQL Delivery API Key
    To access the Sitecore XM Cloud Delivery API, you have to generate the API key for XM Cloud, and for this first you have to request a JWT for Experience Edge XM using OAuth:

    Request an access token for the Experience Edge APIs by using a POST request
    For example, request the JWT using the curl client:
    curl --request POST --url "https://auth.sitecorecloud.io/oauth/token" --header "content-type: application/x-www-form-urlencoded" --data grant_type=client_credentials --data client_id=<clientid> --data client_secret=<clientsecret> --data audience=https://api.sitecorecloud.io
                  

    In this you have the pass the end-point as https://auth.sitecorecloud.io/oauth/token instead of https://one-sc-production.eu.auth0.com/oauth/token, and audience url as https://api.sitecorecloud.io instead of https://delivery.sitecore.cloud/[tenant-id].

    Currently, the url mentioned at Sitecore official documentation Request a JWT for Experience Edge XM using OAuth are incorrect, and team is working on to correct these urls.

    For example, request the JWT using the POSTMAN client:
    You can generate client id and client secret for your Sitecore XM Cloud instance by following the steps present at Generate an Edge administration client for an XM Cloud environment.


    After getting the JWT token you can follow the steps at Create the API key to generate the Sitecore XM Cloud Delivery API Key and you can check sample postman request details to generate the Sitecore XM Cloud Delivery API Key:

Credit/References:
Managing XM Cloud authentication clients Create the API key for Sitecore XM Cloud Sitecore GraphQL Query Example 
Request a JWT for Experience Edge XM using OAuth Sitecore GraphQL Cheat Sheet What is Sitecore Experience Edge?
XM Cloud Introduction How to optimize your omnichannel content strategy Sitecore Slack Conversations
Sitecore Content Hub Edge GraphQL IDE Sitecore Authoring and Management GraphQL API Sitecore Experience Edge for XM Preview GraphQL endpoint 
Sitecore Experience Manager Deliver headless content at scale with Sitecore Experience Edge How to optimize your omnichannel content strategy
Sitecore Content Hub One Sitecore Headless Services and APIs Sitecore GraphQL (GQL)
Sitecore Experience Edge for XM Experience Edge for XM APIs Overview of headless development with Sitecore 
Experience Edge for Content Hub Sitecore Experience Edge for Content Hub Limitations and restrictions of Experience Edge for XM Cloud
Sitecore Experience Edge for XM Cloud Preview GraphQL endpoint The Experience Edge schema for Sitecore XM Sitecore Experience Edge for XM Cloud
Test queries of Sitecore Experience Edge for XM Cloud Experience Edge for XM APIs Sitecore Content Hub ONE Documentation
Experience Edge for XM sitecore experience edge connector sitecore experience edge architecture

Pingback:
Query examples - Sitecore Documentation Start using Sitecore GraphQL API - Sitecore Documentation Test your queries 
Useful example of GraphQL Query in Sitecore context GraphQL Search Query non-Edge What built-in/standard fields are available on the Sitecore GraphQL Search Query
Create / Update / Delete items in Sitecore via GraphQL GraphQL Component Query does not effect Layout Service  Sitecore GraphQL query - custom image prefix
Challenges with Sitecore's GraphQL tooling How do I write a query in Sitecore? How to Write GraphQL Query for Sitecore JSS?
GraphQL and Sitecore experience edge sitecore Prefetch Sitecore data with React Query 
GraphQL for (Headful) Sitecore Queries and Mutations - GraphQL Making GraphQL requests on your frontend with Next.js
Starting with Sitecore JSS Integrated GraphQL How to build a simple navigation with Sitecore JSS using GraphQL sitecore graphql
sitecore graphql endpoint sitecore graphql playground sitecore graphql ui 
sitecore support jobs sitecore graphql search query sitecore graphql authentication required
sitecore graphql example sitecore graphql pagination sitecore graphql api
sitecore graphql cache sitecore graphql schema sitecore graphql server cannot be reached
sitecore graphql configuration experience accelerator sitecore sitecore 8 end of life
sitecore 9 end of life sitecore graphql contextitem sitecore experience edge api  
Building custom Sitecore images sitecore component graphql query could not find type sitecore.services.graphql.dependency configurator
sitecore content hub graphql sitecore xconnect api sitecore graphql datasource
sitecore graphql extender sitecore graphql editable sitecore enable graphql
sitecore jss graphql example sitecore graphql root element is missing sitecore xconnect data analysis
sitecore graphql search examples sitecore graphql filter sitecore graphql fields
sitecore headless graphql
sitecore integrated graphql sitecore install graphql sitecore jss graphql tutorial
sitecore graphql mutation sitecore graphql Fragment Secure Sitecore JSS Site sitecore nextjs graphql
Sitecore Graphql tutorial Sitecore Performance Tuning .net core 6 graphql
sitecore 9 exam questions and answers sitecore xconnect data analytics Speed Up Sitecore Upgrade with PackageReference
sitecore graphql edge Sitecore Version 10 - Sitecore Upgrade Process sitecore graphql api
sitecore xconnect data azure sitecore xconnect data already exists contact sitecore.xconnect.xdbsearchunavailableexception
sitecore xconnect data backup sitecore analytics disable sitecore analytics
enable sitecore analytics tracker sitecore analytics testing tools sitecore analytics vs google analytics
sitecore analytics database manager how to enable sitecore analytics sitecore analytics reports
sitecore analytics not working sitecore analytics cookie sitecore analytics custom reports
sitecore analytics api sitecore analytics ip address sitecore.analytics.tracker.current.session.identify as
sitecore and google analytics adobe analytics integration with sitecore sitecore analytics benefits
best sitecore websites sitecore analytics power bi sitecore analytics chrome extension
sitecore analytics.clustername sitecore analytics.cookiedomain sitecore analytics configuration
sitecore custom analytics sitecore commerce analytics sitecore.analytics.tracker.current is null
Start using Sitecore GraphQL API Sitecore Helix Recommendation and Conventions - Helix 2.0 What’s new in Sitecore 10
Analysis for Sitecore Experience Accelerator (SXA) based website implementation Secure Sitecore JSS Site Sitecore Experience Accelerator (SXA)
Sitecore Graphql tutorial Sitecore Performance Tuning Sitecore GraphQl Examples
What is SXA Page Design? Sitecore Installation and Upgrade Guides Sitecore Upgrade Services

Comments

Popular posts from this blog

Sitecore GraphQL Queries

Configuring Sitecore Next.js Headless SXA Multisite App in a Sitecore Container