Devtantra Blog

Tutorials about Android, Angular, ReactJS, PHP, MySQL and Web Development

Introducing Magento 2.X with GraphQL Module

Magento is an ecommerce platform built on open source technology which provides online merchants with a flexible shopping cart system, as well as control over the look, content and functionality of their online store. Magento offers powerful marketing, search engine optimization, and catalog-management tools. We believe that Magento is one of the best ecommerce platforms available today, with editions ranging from community open source, to massive, large-scale enterprise SaaS based systems.

 

GraphQL Module

In his meduim blog graphan’s announced the first integration with market solutions! The product chosen to be integrated with GraphQL is Magento2 Community Edition .You can require it in composer.json as a normal Magento2 module!

Why Magento2?

E-commerce is one of graphan’s point of interest, so he wanted to add GraphQL intergration into one of popular e-commerce solutions. He has chosen Magento2 CE due to the fact that it is open-sourced and provides much better architecture comparing to Magento 1.x versions.

BENEFITS OF USING MAGENTO

  • Easy to install and add additional layouts and plug-ins
  • Open source technology that offers flexible, scalable ecommerce solutions
  • Effective and cost sensitive program
  • Allows for various discounts and promotions during check-out
  • Provides more than 50 payment gateways

How can I install it?

You can just install it like a normal Magento2 module. Just execute:

composer require we-like-graphql/magento2

and thats all! GraphQL API is already exposed at:

http://<your_magento2_path>/index.php/graphql

Types

GraphQL is strongly typed – you define the exact structure of what can be returned. For example,

type Product {
    id: Int
    sku: String
    title: String
    description: String
    . . .
}

In the case of Magento, types would be generated automatically from the set of available attributes for entities such as “Product”, or from the return type of service contracts.

Interoperability

The simplicity of the GraphQL type system makes it easier to integrate with a wider range of programming languages. It also aligns well with existing Magento 2 service contracts. This makes it straightforward to add GraphQL support to Magento 2 alongside REST and SOAP.

There are several client-side JavaScript libraries available for integrating GraphQL with JavaScript rendering frameworks. For example, Facebook backs Relay for use with React. There is also Apollo. These libraries have various features such as automatic client side caching of data.

Data Retrieval

GraphQL supports concepts of both read request (queries to read data) and write requests (mutations allow inserting/updating/deleting database contents). When you specify a query to retrieve data, you must also specify what fields you want to return. This aligns well with the new proposed Magento query API (a new persistence layer for Magento to separate database access from business logic), where we want to minimize data to retrieve and transfer.

{
    product(sku: MJ01”) {
        sku
        title
    }
}

Data is returned in JSON format.

{
    data”: {
        product”: {
            sku”: MJ01”,
            title”: Mens Jacket in leather
        }
    }
}

Note that fields map on to function calls. For example, the “sku” field above may be a call to a simple getter function of a product record fetched from the database. But, fields can also invoke any defined code, such as a “relatedProducts” field returning an array of product information about related products. This allows a query to fetch data from multiple nodes in a graph of information in a single query. It is this sort of graph navigation that makes GraphQL more powerful than REST.

{
    product(sku: MJ01”) {
        title
        relatedProducts {
            sku
            title
        }
    }
}

Asking for specific data fields may feel burdensome initially, but there are multiple benefits:

  • It reduces network transfer, which can be important for mobile devices.
  • It allows for performance improvements, as the server knows exactly what fields are wanted, enabling query optimizations to be made. (In the case of Magento 2, SQL joins can be avoided for EAV attributes that the client does not need returned.)
  • It allows for schema evolution. New fields (including new linkages between types) can be added without breaking existing applications. Old fields can be removed when no old clients request that field. (GraphQL also supports the concept of marking fields as deprecated for documentation purposes.)

What part of Magento2’s REST APIs is already covered?

The list of covered Magento2’s REST APIs is listed in Readme in this repo. The list is not quite enormous yet, so…

What’s further?

For sure, we want to keep this project active and we will try to cover next Magento2 REST APIs. However, in the future, we would like also to create a demo app that would demonstrate how Magento2 GraphQL APIs can be consumed. We will divide our free time between these tasks.

Check this repo on GitHub. Also you can read this tuts will help your magento site more fast.

10
0

Leave a Reply

%d bloggers like this: