Get CMS Pages collection using Graphql Magento 2.

Get All CMS pages using GraphQl in Magento 2 by a simple module.

We can get all the CMS Pages collection by GraphQl by just create the Resolver model and add our custom logic for getting all the CMS Pages collection. You can add a filter to the condition for getting specific CMS Pages in the Resolver PHP file.

If you want to know Out of the Box Magento cmsPage Query, Refer a link with more explanation, Cms Page, and Cms Blocks GraphQL Query in Magento. 

I hope you are aware of What is GraphQl and how GraphQL is used for the programming language like Magento 2 If You are new to GraphQL check the link for GraphQl in Magento.

Now we can start the module using Magento 2 for fetch all the CMS Pages.

You need to create the first registration.php and module.xml file for defining our module.

I have taken Packagename as Rbj and Modulename as CmsPagesGraphQl.

Path: app/code/Rbj/CmsPagesGraphQl/registration.php

Create module.xml file, Path: app/code/Rbj/CmsPagesGraphQl/etc/module.xml

Our Module depends on GraphQL and CMS Module so we have given the dependency on module.xml file for each module.

Every GraphQl Module must contain schema.graphqls file under the etc folder of a module.

schema.graphqls is at the center of any GraphQL Query implementation and describes what data can be queried. So You can define your query data basic skeleton under the schema.graphqls file.

Path: app/code/Rbj/CmsPagesGraphQl/etc/schema.graphqls

Now Create a Resolver Model for our custom module.
CmsBlock Resolver Model class which is defined in schema.graphql file at above. In this Model, the resolve() method will simply return data of all the CMS Pages.

Path: app/code/Rbj/CmsPagesGraphQl/Model/Resolver/CmsPages.php

Now Run Upgrade command to install our module.

You can check your GraphQL query response by installing chrome extension ChromeiQL or Altair GraphQL addon.

You can check the query results in a browser using Google Chrome ChromeiQl addOn.

cms pages graphql
cms pages graphql

In Request Body, You need to pass the required data(field) to request payload for getting a response of CMS Pages,

In Request Body, You need to pass the required data(field) to request payload for getting a response of CMS Pages,

Request Payload

Result: The result will be all CMS Pages collection,

An Interesting Post about fetch CMS Blocks collection using GraphQl query, Get CMS Static block collection in GraphQL Magento 2