Magento 2 How to get CMS Block in GraphQL
All developers know that CMS Blocks can be used to display promotional banners, sale blocks, return policies, or important information messages on some sections of the store, etc. In this tutorial, we'll discuss how to get CMSBlocks in GraphQL. The CMSBlocks query returns information about blocks.
Syntax
Get only one CMSBlock
saveCopyzoom_out_mapcmsBlocks(identifiers: String): CmsBlocks
Get multiple CMSBlock
saveCopyzoom_out_mapcmsBlocks(identifiers: [String,String]): CmsBlocks
The following query returns information about "Login Data" CMSBlock.
Request
{
cmsBlocks(identifiers: "login-data") {
items {
identifier
title
content
}
}
}
Response
{
"data": {
"cmsBlocks": {
"items": [
{
"identifier": "login-data",
"title": "Login Info Block",
"content": "<div class=\"message info\" style=\"margin-top: 50px;\">\n <p><strong>Try Demo Customer Access</strong></p>\n <p><span style=\"display:inline-block; width: 80px; padding-right: 10px;\">Email:</span>roni_cost@example.com</p>\n <p><span style=\"display:inline-block; width: 80px; padding-right: 10px;\">Password:</span>roni_cost3@example.com</p>\n</div>"
}
]
}
}
}
That's it!!
If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.
I hope this article helps you to find what you were looking for.
Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this article with your team.