Skip to content

Instantly share code, notes, and snippets.

@AmitKumar-AK
Last active January 24, 2021 09:05
Show Gist options
  • Save AmitKumar-AK/6976d63bfc5933b9b9682e7815487104 to your computer and use it in GitHub Desktop.
Save AmitKumar-AK/6976d63bfc5933b9b9682e7815487104 to your computer and use it in GitHub Desktop.
Sitecore GraphQL Queries Usage -This file contains a Sitecore GraphQL queries that will be executed and the result provided to your Sitecore JSS component. You can run this query in Siteocre Experience Graph Browser/GraphiQL/xGraph Browser ($endpoint/ui) for a nice editing experience.
# Sitecore GraphQL Queries Usage -This file contains a Sitecore GraphQL queries that will be executed and the result provided to your Sitecore JSS component.
# You can run this query in Siteocre Experience Graph Browser/GraphiQL/xGraph Browser ($endpoint/ui) for a nice editing experience.
#---------Use this part for Query Example-----------::Start::------------
#Current context of item
query GQLQuerySamples( $contextItem: String!) {
#----Get an item by path and show field id and name:: Start ::------
GQLType1: item(path: $contextItem) {
id
name
}
#----Get an item by path and show field id and name:: End ::------
#----Custom name for a field, test contain the id value:: Start ::------
GQLType2: item(path: $contextItem) {
id
name
EmployeId : id
}
#----Custom name for a field, test contain the id value:: End ::------
#----Return the name and value from all fields:: Start ::------
GQLType3: item(path: $contextItem) {
fields {
name
value
}
}
#----Return the name and value from all fields:: End ::------
#----Return a specific field, in this case custom field "firstname" with Alias:: Start ::------
GQLType4: item(path: $contextItem) {
FName:field(name : "firstname") {
title: value
}
MName:field(name : "middlename") {
title: value
}
LName:field(name : "lastname") {
title: value
}
}
#----Return a specific field, in this case custom field "firstname" with Alias:: End ::------
#----Get a children: You can access only common values not :: Start ::------
#child specific values
GQLType5: item(path: $contextItem) {
children
{
id
name
}
}
#----Get a children: You can access only common values not :: End ::------
#----Get a typed safe data on the basis of template :: Start ::------
GQLType6: item(path: $contextItem) {
children {
... on RelatedDoctors
{
id
name
}
}
}
#----Get a typed safe data on the basis of template :: End ::------
#----Get only specific children on the basis of Template :: Start ::------
GQLType7: item(path: $contextItem) {
children (includeTemplateIDs: ["{3D4EEE9F-29D5-42F5-B317-3E932F74693D}"]){
... on RelatedDoctors
{
id
name
}
}
}
#----Get only specific children on the basis of Template :: End ::------
#----Get value of multi-list field from item :: Start ::------
GQLType8: item(path: $contextItem) {
children (includeTemplateIDs: ["{3D4EEE9F-29D5-42F5-B317-3E932F74693D}"])
{
... on RelatedDoctors
{
id
name
doctors
{
DoctorList:targetItems
{
... on Doctor
{
DoctorId: id
DoctorName: name
firstName {
id
value
#The editable value of this field. If not in edit mode, this value is identical to
#'rendered'. This value may contain raw HTML; do not encode when displaying
editable
}
#Gets the field value rendered for use in Sitecore JSS field helper components
lastName
{
jss
}
headshot
{
value
src
alt
name
displayName
description
jss
}
}
}
}
}
}
}
#----Get value of multi-list field from item :: End ::------
#----Get children of multi-list field from child item of current context :: Start ::------
GQLType9: item(path: $contextItem) {
ChildLevel_1:children (includeTemplateIDs: ["{3D4EEE9F-29D5-42F5-B317-3E932F74693D}"])
{
... on RelatedDoctors
{
id
name
doctors
{
DoctorList:targetItems
{
... on Doctor
{
DoctorId: id
DoctorName: name
ChildLevel_2:children (includeTemplateIDs: ["{3D4EEE9F-29D5-42F5-B317-3E932F74693D}"])
{
id
name
}
}
}
}
}
}
}
#----Get children of multi-list field from child item of current context :: End ::------
#----Get the values of Treelist field type :: Start ::------
GQLType10: item(path: $contextItem) {
id,
name
... on Doctor
{
services
{
targetItems
{
... on Service
{
pageTitle {
value
}
pageDescription {
value
}
}
}
}
}
}
#----Get the values of Treelist field type :: End ::------
#----Get the template field details with values of current item :: Start ::------
GQLType11: item(path: $contextItem) {
id
name
__typename
... on Item
{
fields {
id
name
}
}
}
#----Get the template field details with values of current item :: End ::------
#----Get the field values using Aliases :: Start ::------
GQLType12: item(path: $contextItem) {
DoctorId:id,
... on Doctor
{
FName: firstName {
value
}
LName: lastName {
value
}
}
}
#----Get the field values using Aliases :: End ::------
#----Get List of all language versions for particular item :: Start ::------
GQLType13: item(path: $contextItem) {
versions(allLanguages: true)
{
name,
language {
name
}
}
}
#----Get List of all language versions for particular item :: End ::------
# End of GQLQuerySamples
}
#---------Use this part to test Fragment Query Example-----------::Start::------------
##################################################
query GQLQuerySamples( $contextItem: String!) {
#Get repeated fields using Fragments:
GQLType14: item(path: $contextItem) {
id
name
children (includeTemplateIDs: ["{3D4EEE9F-29D5-42F5-B317-3E932F74693D}"]) {
name
... on RelatedDoctors
{
doctors {
...Doctors
}
}
}
}
}
fragment Doctors on MultilistField {
DoctorList:targetItems {
name
id
... on Doctor
{
firstName {
value
jss
}
lastName {
value
}
headshot
{
value
src
alt
name
displayName
description
jss
}
location{value}
services
{
...ServicesList
}
...RelatedDocs
}
}
}
fragment ServicesList on MultilistField {
ServiceList:targetItems {
... on Service
{
pageTitle {
value
}
pageDescription {
value
}
}
}
}
fragment RelatedDocs on Item {
children (includeTemplateIDs: ["{3D4EEE9F-29D5-42F5-B317-3E932F74693D}"])
{
... on RelatedDoctors
{
name
doctors
{
DoctorList:targetItems{
name
id
... on Doctor{
firstName {
value
jss
}
lastName {
value
}
headshot
{
value
src
alt
name
displayName
description
jss
}
location{value}
services
{
...ServicesList
}
}
}
}
}
}
}
##################################################
#---------Use this part to test Fragment Query Example-----------::End::------------
#Query Variable
#{
# "contextItem": "/sitecore/content/patient-core/home/Doctors/Dr Ninja"
#}
#---------Use this part for Query Example-----------::End::------------
#---------Use this part for Sitecore GQL Search Query Example-----------::Start::------------
# Sitecore GraphQL Search Query Keyword and rootItem usage
{
search(keyword:"GraphQL"
rootItem:"/sitecore/content"
) {
results {
totalCount,
items{
id
name
path
}
}
}
}
# Sitecore GraphQL Search Query fieldsEqual parameter and explicit usage of
# field name property
{
search(
fieldsEqual:[{name:"firstName", value:"A*" }] rootItem:"/sitecore/content/patient-core/home/Doctors"
) {
results {
items {
item {
id
name
path
url
field(name : "firstName") {
title: value
}
}
}
totalCount
}
}
}
# Sitecore GraphQL Search Query fieldsEqual parameter and explicit usage of
# field name property with alias usage
{
search(
fieldsEqual:[
{name:"firstName", value:"*A*" }
{name:"lastName", value:"*K*" }
]
rootItem:"/sitecore/content/patient-core/home/Doctors"
) {
results {
items {
item {
id
name
path
url
FirstName: field(name : "firstName") {
value
}
LastName: field(name : "lastName") {
value
}
}
}
totalCount
}
}
}
# Sitecore GraphQL Search Query fieldsEqual, facetOn parameter and explicit usage of
# field name property with alias and facets usage
{
search(
fieldsEqual:[
{name:"firstName", value:"*A*" }
{name:"lastName", value:"*K*" }
]
rootItem:"/sitecore/content/patient-core/home/Doctors"
facetOn:["services"]
) {
results {
items {
item {
id
name
path
url
FirstName: field(name : "firstName") {
value
}
LastName: field(name : "lastName") {
value
}
}
}
totalCount
}
facets {
FacetName:name
values
{
count
item {
FacetId:id
# Get fields value of Facet item using GUID
... on Service
{
FacetValue:pageTitle {
value
}
}
}
}
}
}
}
# Sitecore GraphQL Search Query keyword, facetOn parameter and
# how to query on Sitecore Templates
{
search(keyword:"GraphQL"
rootItem:"/sitecore/content"
facetOn:["_template"]
) {
results {
totalCount,
items{
id
name
path
}
}
facets {
FacetName:name
values
{
count
item {
FacetId:id
name
displayName
}
}
}
}
}
#---------Use this part for Sitecore GQL Search Query Example-----------::End::------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment