GraphQL
GraphQL is a query language for APIs that streamlines development so you can fetch specific content and deliver it to any channel and you only retrieve the content you need. It improves efficiency by letting you combine multiple data sources in a single query, and receive the requested data in a single response.
Sitecore GraphQL is available for various APIs. The data structure, or schema, is unique to the implementation - for example, Sitecore Content Hub uses a bespoke schema. As shown in the following diagram, client channels use GraphQL queries to request data from the data sources at the bottom.
Using GraphQL, you can query data using available APIs such as the Preview API and the Delivery API. Sitecore optimizes GraphQL for speed, letting you reduce network traffic by batching multiple queries in a single HTTP request.
The Preview API and Delivery API do not support mutations.
GraphQL endpoints host a schema (a strongly-typed graph definition) and understand the GraphQL language. Unlike REST APIs, there is a formal request and response format. Each endpoint has its own isolated configuration, so endpoints can be isolated from any other endpoint configuration to increase reliability.
The GraphQL query editor is built into Sitecore GraphQL endpoints so that you can author queries (with code completion) and review API documentation. The GraphQL type system knows about Sitecore templates so that you can create and validate strongly typed queries against real fields. Template changes are updated in real-time and mapped to GraphQL types.
Structure of a GraphQL query
GraphQL queries use a specific structure to represent the requested data. Each request is a standard POST request with a query in the body. The main elements of a GraphQL query are:
-
Field - a named field in the target data (such as
total
,results
,id
, and so on). -
Argument - a value associated with a specific field (such as
id_eq: "Ai-JNhneM024pMItfBeYJQ
). -
Operation type - the type of operation (that is, query, mutation, or subscription).
-
Operation name - the name of the operation (such as
getContentInContentCollection
). -
Variable - a dynamic parameter attached to the operation.
The following example represents a query operation named getContentInContentCollection
. In this example, the query looks for a content collection that has an id of Ai-JNhneM024pMItfBeYJQ
, and fetches multiple field values for that collection including id
, contentCollectionName
, and specific contents of the contentCollectionToContent
object.
query getContentInContentCollection {
m_ContentCollection(id: "Ai-JNhneM024pMItfBeYJQ") {
id
contentCollectionName
contentCollectionToContent {
total
results {
content_Name
}
}
}
}
This query returns the following response data, which has the same overall structure as the query itself.
{
"data": {
"allM_ContentCollection": {
"total": 1,
"results": [
{
"id": "Ai-JNhneM024pMItfBeYJQ",
"contentCollectionName": "Fruitful",
"contentCollectionToContent": {
"total": 4,
"results": [
{
"content_Name": "Healthy Mimosa blog",
},
{
"content_Name": "Healthful is the latest Flavorful Brand",
},
{
"content_Name": "Sparkling Punch using Fruitful Orange",
},
{
"content_Name": "Power comes from within",
}
]
}
}
]
}
}
}