The examples of some GraphQL queries for the Magento 2 blog module are presented in this article. With these queries, you will be able to extract the necessary blog data for Progressive Web Application (PWA). The queries are easily tested in the ChromeiQL - Chrome Browser Extension.

Note that Magento 2 Blog GraphQL addition should be installed first.

To check the full Blog GraphQL schema please see the schema.graphqls file.

Blog Index Page Posts GraphQL Request

Query:

query GetPosts ($currentPage: Int $pageSize: Int) {
    blogPosts (currentPage: $currentPage pageSize: $pageSize sort: ["DESC"]) {
        total_count
        items {
            post_id
            title
            short_filtered_content
            author {
                author_id
                name
                author_url
            }
            post_url
            creation_time
            tags {
                tag_id
                title
                tag_url
            }
            categories {
                category_id
                title
                category_url
            }
        }
        total_pages
    }
}

Variables:

{
"currentPage": 1,
"pageSize": 5
}

Result:

As a result, you will receive data for the first 5 blog posts that should be displayed on the first page.

Index graphql test

Blog Category Posts GraphQL Request

With this query you will get posts for a certain blog category, using the category ID or category URL identifier.

Query:

blogPosts (currentPage: $currentPage pageSize: $pageSize sort: ["DESC"] filter: {category_id: {eq: $categoryId}}) {
    total_count
    items {
        post_id
        title
        short_filtered_content
        author {
            author_id
            name
            author_url
        }
        post_url
        creation_time
        tags {
            tag_id
            title
            tag_url
        }
        categories {
            category_id
            title
            category_url
        }
    }
    total_pages
}
blogCategory (id: $categoryId) {
    title
    content
}
}

Variables:

{
"currentPage": 1,
"pageSize": 5,
"categoryId":"fashion-show"
}

Result:

Category graphql test

Blog Author's Posts GraphQL Request

With this query you will get the author's posts data, using the author ID or author URL identifier.

Query:

query GetPosts ($currentPage: Int $pageSize: Int $authorId: String) {
blogPosts (currentPage: $currentPage pageSize: $pageSize sort: ["DESC"] filter: {author_id: {eq: $authorId}}) {
    total_count
    items {
        post_id
        title
        short_filtered_content
        author {
            author_id
            name
            author_url
        }
        post_url
        creation_time
        tags {
            tag_id
            title
            tag_url
        }
        categories {
            category_id
            title
            category_url
        }
    }
    total_pages
}
blogAuthor(id: $authorId) {
    name
}
}

Variables:

{
"currentPage": 1,
"pageSize": 5,
"authorId":"store-administrator"
}

Result: 

Author graphql test

Blog Posts Tags GraphQL Request

With this query you will get the list of posts containing the corresponding tag, using the tags ID or tag URL identifier.

Query:

query GetPosts ($currentPage: Int $pageSize: Int $tagId: String) {
    blogPosts (currentPage: $currentPage pageSize: $pageSize sort: ["DESC"] filter: {tag_id: {eq: $tagId}}) {
        total_count
        items {
            post_id
            title
            short_filtered_content
            author {
                author_id
                name
                author_url
            }
            post_url
            creation_time
            tags {
                tag_id
                title
                tag_url
            }
            categories {
                category_id
                title
                category_url
            }
        }
        total_pages
    }
    blogTag (id: $tagId) {
        title
        content
    }
}

Variables:

{
"currentPage": 1,
"pageSize": 5,
"tagId": "bags"
}

Result:

Tags graphql test

Search Posts GraphQL Request

With this query, you will get the list of posts related to the word you are looking for.

Query:

query GetPosts ($currentPage: Int $pageSize: Int $searchQuery: String) {
        blogPosts (
            currentPage: $currentPage 
            pageSize: $pageSize 
            sort: ["DESC"]
            filter: {search: {eq: $searchQuery}}
        ) {
            total_count
            items {
                post_id
                title
                short_filtered_content
                author {
                    author_id
                    name
                    author_url
                }
                post_url
                creation_time
                tags {
                    tag_id
                    title
                    tag_url
                }
                categories {
                    category_id
                    title
                    category_url
                }
            }
            total_pages
        }
    }

Variables:

{
"currentPage": 1,
"pageSize": 5,
"searchQuery:":"good"
}

Result:

Search graphql test

Single Blog Post Data

With this query, you will be able to get the blog post by post ID or URL identifier.

Query:

query GetPost ($postId: String) {
                blogPost (id: $postId) {
                    post_id
                    meta_title
                    meta_description
                    title
                    filtered_content
                    author {
                        author_id
                        name
                        author_url
                    }
                    post_url
                    creation_time
                    tags {
                        tag_id
                        title
                        tag_url
                    }
                    categories {
                        category_id
                        title
                        category_url
                    }
                }
            }

Variables:

{
"postId": "kaytips-when-in-dubai"
}

Result:

Post graphql test

Sidebar Categories GraphQL Request

With this query, you will be able to get the blog categories list.

Query:

{
    blogCategories {
        items {
            category_id
            title                        
            category_url
        }
    }
}

Result:

GraphQL sidebar category test

Sidebar Recent Posts GraphQL Request

With this query, you will be able to get the blog recent posts.

Query:

query GetPosts ($pageSize: Int) {
    blogPosts (pageSize: $pageSize sort: ["DESC"]) {
        items {
            post_id
            title
            post_url
        }
    }
}

Result:

GraphQL resent posts test

Sidebar Tags GraphQL Request

 

With this query, you will be able to get the blog tags.

Query:

{
    blogTags {
        items {
            tag_id
            title
            tag_url
        }
    }
}

Result:

GraphQL sidebar tags test