Magento GraphQL - are you sure you know when to use it? - Bartosz Herba, Senior Developer at Divante
Presentation originally presented at Magento Lightning Talks meetup on October 3rd, 2019, in Divante HQ.
Learn more at Divante at https://divante.com
Check out more Magento Lightning Talks at https://divante.com/blog/tag/magento-lightning-talks/
1. Bartosz Herba | Divante Senior Developer
GraphQL
are you sure you know when to use it?
2. Let’s talk about
eCommerce
Hi, my name is Bartosz Herba
I am a Magento 2 developer with a few years of experience in working on
e-commerce solutions
email: bherba@divante.co
mobile: +48 662 093 917
3. What is a GraphQL?
What is a GraphQL
● Invented and used by Facebook since 2012 to speed up
communication with a facebook API for a different kind of
devices.
● GraphQL is an Alternative, Elastic, Resource Effective,
Centralized and Strongly Typed way of a communication with
an API
4. What is a GraphQL?
Alternative
● Does not replace REST or SOAP in the “way” of communication with an API,
just gives another tool to achieve similar goal.
● Brings new structured approach of how we ask for a required resources
5. What is a GraphQL?
Alternative
Query Body Response
Endpoint Url
6. What is a GraphQL?
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
7. What is a GraphQL?
Elastic and Resource Effective
{"id":1,"sku":"simple_sku","name":"Test
Simple","attribute_set_id":4,"price":119,"status":1,"visibility":4,"type_id":"simple","created_at":"2019-09-19
10:02:25","updated_at":"2019-09-19
10:02:25","extension_attributes":{"website_ids":[1],"stock_item":{"item_id":1,"product_id":1,"stock_id":1,"qty":12,"is_in_stock":true,"i
s_qty_decimal":false,"show_default_notification_message":false,"use_config_min_qty":true,"min_qty":0,"use_config_min_sale_qty":1
,"min_sale_qty":1,"use_config_max_sale_qty":true,"max_sale_qty":10000,"use_config_backorders":true,"backorders":0,"use_config_
notify_stock_qty":true,"notify_stock_qty":1,"use_config_qty_increments":true,"qty_increments":0,"use_config_enable_qty_inc":true,"
enable_qty_increments":false,"use_config_manage_stock":true,"manage_stock":true,"low_stock_date":null,"is_decimal_divided":fals
e,"stock_status_changed_auto":0}},"product_links":[],"options":[],"media_gallery_entries":[],"tier_prices":[],"custom_attributes":[{"attri
bute_code":"pimcore_id","value":"0"},{"attribute_code":"is_active_in_pim","value":"0"},{"attribute_code":"options_container","value":
"container2"},{"attribute_code":"msrp_display_actual_price_type","value":"0"},{"attribute_code":"url_key","value":"test-simple"},{"attr
ibute_code":"gift_message_available","value":"2"},{"attribute_code":"required_options","value":"0"},{"attribute_code":"has_options",
"value":"0"},{"attribute_code":"meta_title","value":"Test Simple"},{"attribute_code":"meta_keyword","value":"Test
Simple"},{"attribute_code":"meta_description","value":"Test Simple
"},{"attribute_code":"tax_class_id","value":"2"},{"attribute_code":"category_ids","value":[]}]}
Rest - simplest possible response is full of unnecessary data
8. What is a GraphQL?
Elastic and Resource Effective
{
"data": {
"products": {
"items": [
{
"price": {
"regularPrice": {
"amount": {
"value": 119
}
}
},
"sku": "simple_sku",
"name": "Test Simple",
"image": {
"label": "Test Simple"
...
GraphQL - you get exactly what you want and nothing more
9. What is a GraphQL?
Elastic and Resource Effective
155
1600
10. What is a GraphQL?
Centralized
● There is just one endpoint for any request
● Difference is in 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
12. What is a GraphQL?
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.
13. What is a GraphQL?
When to use it?
● Building an API for different devices
● Building an open API
● Working with Model-View layers
● Working with multiple-resources-at-a-time scenarios
14. What is a GraphQL?
When not to use it?
● I would not recommend to use it with ERP integrations
● But, to be frankly, it is hard to give a general and true statement on such a
topic
15. What is a GraphQL?
GraphQL in Magento
Available from 2.3.x
Main target is PWA