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

We already have REST architecture 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.

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 enitre 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
query cmsPage{
  cmsPage(id: 5){
    url_key
    title
    content
    content_heading
    page_layout
    meta_title
    meta_description
    meta_keywords
  }
}

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.

query cmsPage{
  cmsPage(id: 5){
    url_key
    title
  }
}

The above query resulted like below.

GrapgQl query with the result
GrapgQl query with the result

So this is some wrap-up about GraphQl in magento 2.3. 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.

Leave a Reply

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