Introduction
In the dynamic world of web development, efficiency and performance are paramount. One powerful way to achieve both is by integrating GraphQL with Sitecore, a leading enterprise content management system. While GraphQL isn’t new to Sitecore, the advent of Sitecore JavaScript Services (JSS) has made its implementation smoother and more accessible than ever.
This guide walks you through the process of setting up GraphQL in Sitecore, focusing on how to craft effective queries to retrieve content. Whether you're building a new solution or enhancing an existing one, this integration can be a game-changer.
Why GraphQL with Sitecore?
Integrating GraphQL into Sitecore offers a host of benefits:
- Enhanced Flexibility: Fetch only the data you need—no more, no less.
- Streamlined Development: Decouple frontend and backend workflows for faster iteration.
- Improved Performance: Reduce payload sizes and speed up response times.
- Modern Frontend Compatibility: GraphQL’s schema-based approach aligns perfectly with frameworks like React, Vue.js, and Angular.
Getting Started: Installing JSS and GraphQL
To begin, you’ll need the correct JSS server component package for your Sitecore version. Once identified:
Step 1: Install the JSS Package
- Open Sitecore in your browser.
- Navigate to:
Desktop → Development Tools → Installation Wizard
. - Upload and install the JSS package.
This installation will add several components to your Sitecore instance:
- Configuration files under
App_Config
(including LayoutService and GraphQL configs). - DLLs in the
bin
folder. JssApp.cshtml
view file.- Sitecore templates under
Foundation
andSystem
.
Step 2: Configure GraphQL
After installation:
- Go to
App_Config\Services.GraphQL
. - Rename the file
Sitecore.Services.GraphQL.Content.Master.config.example
toSitecore.Services.GraphQL.Content.Master.config
.
Step 3: Add and Configure API Key
To query GraphQL via API:
- Switch to the Core database.
- Navigate to
/sitecore/system/Settings/Services/API Keys
. - Right-click and insert a new API Key item.
Enable API Key Authentication
Edit the file:App_Config\Sitecore\Services.GraphQL\Sitecore.Services.GraphQL.config
In the <security>
section, enable API Key authentication.
Step 4: Access the GraphQL Browser
Once everything is set up, you can test your queries using the Sitecore GraphQL Browser:
- URL format:
https://your-domain/sitecore/api/graph/items/master/ui
If you encounter an “Authentication required” error:
Fix: Add Site-Neutral Path
- Open:
App_Config\Sitecore\Owin.Authentication\Sitecore.Owin.Authentication.config
- Locate the
<siteNeutralPaths>
node. - Add:
/sitecore/api/graph/items/
Save the file and retry accessing the GraphQL browser.
Step 5: Run Your First Query
Here’s a sample GraphQL query to fetch child items of a context item:
query GQLQuerySamples($contextItem: String!) {
item(path: $contextItem) {
children {
id
name
}
}
}
Important Notes:
- In the Query Variables section, pass the
contextItem
path. - In the HTTP Headers, include your API Key.
Conclusion
Integrating GraphQL with Sitecore empowers developers to build faster, leaner, and more scalable applications. With precise data fetching, improved performance, and seamless frontend integration, GraphQL is a natural fit for modern Sitecore solutions.
Whether you're a seasoned Sitecore developer or just getting started, embracing GraphQL can elevate your development workflow and deliver richer digital experiences.
I hope you enjoy this Sitecore blog. Stay tuned for more Sitecore related articles.
Till that happy Sitecoring :)
Please leave your comments or share this article if it’s useful for you.