Skip to content
Available for new work
Pages in Blog

GuidePremium

Try the blog GraphQL API in CoullWorks Blog for Magento 2

A task-based walk through the blog's GraphQL queries from a GraphQL client or curl (list posts, read one post, search, categories, SEO data, related products, comments), with the errors you may meet.

Updated 17 November 2026Back to the package pageBuy

This page is for trying the blog's GraphQL queries and for building a headless storefront on them, one task at a time. The fields, arguments and types are listed once, in the GraphQL schema reference, and the overview, the comment flow and the REST routes are on GraphQL and REST. This page does not repeat them.

Before you start#

  • The GraphQL add-on is a separate module, CoullWorks_BlogGraphQl, and needs the Blog installed and licensed. Check that it is enabled:

    text
    bin/magento module:status CoullWorks_BlogGraphQl

    If it is disabled, enable it as described on Install and run bin/magento setup:upgrade.

  • The endpoint is Magento's usual one, https://your-store.example/graphql. Magento does not ship a GraphQL playground screen in the admin and the blog adds none, so use any GraphQL client (a browser extension such as GraphiQL or Altair, Postman, Insomnia) or curl. Because the blog's schema is part of Magento's, a client that loads the schema by introspection lists every cwBlog query and type with its description.
  • Posts are only returned if the blog's search index and cache are current. Run the steps in Blog search once if cwBlogSearch or the search filter return nothing.
  • To read another store view, send the header Store: <store view code>, as for any Magento GraphQL request.

Send your first query#

From a terminal:

text
curl -s https://your-store.example/graphql \
  -H 'Content-Type: application/json' \
  -d '{"query":"{ cwBlogPosts(pageSize: 3) { total_count items { id title url_key } } }"}'

In a client, paste the query on its own and press run:

graphql
{
  cwBlogPosts(pageSize: 3) {
    total_count
    items { id title url_key }
  }
}

total_count is the number of posts the reader can see, not only the three returned. Reads show only what the reader could see on the storefront, which without a customer token means a logged-out visitor: a draft, a scheduled post, a disabled post or a post for another store view is never returned.

Task: list the posts in a category, newest first#

  1. Find the category's id: { cwBlogCategories { id title url_key } } lists the top-level categories, and cwBlogCategory(url_key: "guides") returns one.
  2. Pass it as a filter, with a sort:
graphql
{
  cwBlogPosts(
    filter: { category_id: 3 }
    sort: { field: PUBLISHED_AT, direction: DESC }
    pageSize: 12
    currentPage: 1
  ) {
    total_count
    items { id title url_key short_content featured_image reading_minutes published_at }
    page_info { current_page total_pages }
  }
}

The same filter takes tag_id and author_id. pageSize must be between 1 and 100 and currentPage at least 1, or the query fails with a message saying so. Increase currentPage until it reaches total_pages.

Task: read one post for a page#

Ask for a post by its address key or its id. One of the two is required.

graphql
{
  cwBlogPost(url_key: "tent-pitch-checklist") {
    id
    title
    published_at
    updated_at
    reading_minutes
    featured_image
    featured_image_alt
    content_html
    takeaways
    author { name job_title avatar url }
    categories { title url }
    tags { title url }
  }
}
  • Use content_html to print the article as the storefront does, with widgets and media resolved. Use content_blocks when you want to render the blocks yourself; cwBlogBlockSchema describes their shape.
  • url is the post's canonical address on the Magento storefront.
  • A post that is not visible, or does not exist, answers "The post does not exist." Sending neither url_key nor id answers "Specify url_key or id."

Task: get the SEO data for a post#

A headless page still needs a title, a canonical link and structured data. They are produced by the same code as the Magento storefront's head.

graphql
{
  cwBlogPost(url_key: "tent-pitch-checklist") {
    meta { title description robots canonical og { key value } twitter { key value } }
    json_ld
    hreflang { hreflang href }
  }
}

json_ld is a list of JSON strings, one per document. Print each inside its own <script type="application/ld+json">. What each tag is, and how to switch the ones another module already prints, is on SEO output.

graphql
{
  cwBlogPost(url_key: "tent-pitch-checklist") {
    related_posts { title url_key featured_image }
    related_products { name sku url_key }
  }
}

related_products returns Magento's product type, so any product field your storefront already queries is available. Only products the reader's store and customer group can see are returned.

graphql
{
  cwBlogSearch(query: "tent", pageSize: 5) {
    total_count
    items { title url_key short_content }
  }
}

It uses the same search as the storefront, so it needs the index in the section above. Searching with the search filter of cwBlogPosts is the same search with category_id and tag_id to narrow it. author_id cannot be combined with search, and the query fails with "author_id cannot be combined with search."

Task: build a menu from categories, tags and archive data#

  • Categories, with children: { cwBlogCategories { id title url children { id title url } } }. Pass parent_id to start below a given category.
  • Tags: { cwBlogTags(pageSize: 50) { items { title url_key } total_count } }.
  • Authors: { cwBlogAuthors { name url_key links { network url } } }.

There is no archive query. To list months, query cwBlogPosts sorted by PUBLISHED_AT and group on published_at in your front end.

Task: add a comment#

Read the token, then send the mutation. The steps and a worked example are on GraphQL and REST, and the types are in the schema reference.

When something goes wrong#

What you seeLikely cause
An authorisation error on every queryThe store view's domain is not licensed (see Licence)
Cannot query field "cwBlogPosts"The add-on is not enabled, or setup:upgrade and a cache flush have not been run since it was
pageSize must be between 1 and 100.A page size outside the allowed range
cwBlogPosts returns total_count: 0Posts exist but are drafts, scheduled, or not in the store view named in the Store header
cwBlogSearch returns nothing for a word that is in a postThe search index is empty: run the reindex in Blog search
A change shows on the storefront but not in the responseResponses are cached with the blog's cache tags. Flush the cache, or check that your cache layer passes the tags through (see GraphQL and REST)

Available for new work

UK based · PHP · Python · JS · TS. Every first call is free.