Divante.com Blog

Do you know when to use GraphQL with Magento? In this article, I cover ways in which GraphQL communicates with API and situations when the usage of GraphQL is beneficial for Magento-based systems.

What is GraphQL?

GraphQL was invented and used by Facebook since 2012 to speed up communication with a facebook API for different kinds of devices.

GraphQL is an Alternative, Elastic, Resource Effective, Centralized and Strongly Typed way of communication with an API. Let’s go through these ways:

Alternative

  • Does not replace REST or SOAP in the “way” of communication with an API,
    just gives another tool to achieve a similar goal.
  • Brings a new structured approach of how we ask for a required resources

Elastic and Resource Effective

  • You don’t have to ask for everything but exactly and only what you need
  • Therefore requests and responses are lightweight because only necessary data are transferred
  • No more of under/over fetching problems

Centralized

  • There is just one endpoint for any request
  • The difference is in the request’s body structure
  • It means that you can prepare just a single request instead of many to get all necessary data for a view 

Strongly Typed

  • The most basic components of a GraphQL schema are object types, which just represent a kind of object you can fetch from your service, and what fields it has. 
  • Every field on a GraphQL object type can have zero or more arguments
  • A GraphQL object type has a name and fields, but at some point, those fields have to resolve to some concrete data. That’s where the scalar types come in: they represent the leaves of the query.
  • Scalars, enums, lists, interfaces
  • We always know what to expect.

When to use GraphQL?

Use GraphQL when:

  • Building an API for different devices
  • Building an open API
  • Working with Model-View layers
  • Working with multiple-resources-at-a-time scenarios

When not to use GraphQL?

  • I would not recommend to use it with ERP integrations
  • But, to be frank, it is hard to give a general and true statement on such a topic.

Check out my presentation on GraphQL in Magento for details:


Presentation originally presented at Magento Lightning Talks meetup on October 3rd, 2019, in Divante HQ. Magento Lightning Talks is an event series, organized by Divante, to share and exchange knowledge on Magento development among Magento developers.

Learn more at Divante.

Check out more Magento Lightning Talks.

Bartosz Herba

Senior developer at Divante eCommerce Software House | LinkedIn

Share your comment