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 byGraphQl by just create 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 Resolver PHP file.

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

Check for fetch CMS Blocks collection using GraphQl query, Get CMS Static block collection in GraphQL Magento 2

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

You need to create 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 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 the 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.
Rbj\\CmsPagesGraphQl\\Model\\Resolver\\CmsBlock
CmsBlock Resolver Model class which is defined in schema.graphql file at above. In this Model, 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 query result 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,

 

Leave a Reply

Your email address will not be published. Required fields are marked *