What is GraphQl in magento 2 and how to access it?

What is GraphQl in magento 2 and how to access it? – We already have REST and SOAP protocol in Magento. Magento 2.3 introduced a kind of alternative for these two. That’s GraphQl.
We recently planned an architecture for a project and one of our Senior Tech-lead suggest Magento PWA & GraphQl approach to develop the website. Back-end is totally handled by magento and front end is React.

One of the best in GraphQl than REST i felt was we can simply get the result what we exactly want not like REST’s entire result. For example if we want to get the product price from the url_key.We can request only the price in the GraphQl Query and it returns only the price not like REST the entire result.

How to Access GraphQl?

You can use ChromeiQL or Altair GraphQL add on to access.To check our query we want to set up the endpoint. It is usually coming like <magento_root_url>/graphql. Ex :- http://ayakil.local/graphql.

GraphQl support 2 types of queries

  1. type Query to get/read data.
  2. type Mutation to perform CRUD operations.

Example of the Query currently have in magento( vendor/magento/module-cms-graph-ql)
Just consider we want to get the cms page of id = 3.We want to pass the id to cms graphQl query in the module module-cms-graph-ql. Just consider the cms page id 5.in my localhost that was about us page

About us CMS page in Admin panel
About us CMS page in Admin panel

This query cmsPage have the ability to retrieve url_key,title, content, content_heading, page_layout, meta_title,meta_description,meta_keywords.The above query resulted like below.

GraphQl query and result
GraphQl query and result

But if you want to get any special value among those you can just pass in the query body like below to get the specific result.

The above query resulted like below.

GrapgQl query with the result
GraphQl query with the result

So this is some wrap-up about GraphQl in magento 2. Next article will be How to create a graphql schema for magento 2 custom module with custom table?.

That’s it. Have a nice day.Enjoy coding , Learn , Experience , Teach and Help.

2 thoughts on “What is GraphQl in magento 2 and how to access it?”

Comments are closed.