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?
GraphQl support 2 types of queries
- type Query to get/read data.
- 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
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.
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.
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.