# There and back again ## GraphQL, Magento and React #### by Jisse Reitsma (Yireo)
# Jisse Reitsma - Founder of Yireo - Trainer of developers - Creator of MageTestFest (2017, 2018) - Creator of Reacticon (2018 x2) - Creator of Reacticon v3 (June 2020) - Magento Master 2017/2018/2019 Mover - Member of ExtDN (Magento Extension Developer Network)
# Remote online training - Magento PWA Studio development - Vue Storefront development - GraphQL API microservice development (PHP) - React / Redux / Apollo / SSR - Vue / Vuex / Apollo / SSR Link: yireo.com/events
# GraphQL, Magento and React
# Because GraphQL is da bomb
# Because GraphQL goes viral
# Because GraphQL is da bomb
# GraphQL in a few minutes
# GraphQL in short - Queries, mutations & fragments
# A simple GraphQL query ```js { cmsPage(id:2) { title content } } ```
# A simple JSON response ```js { "data": { "cmsPage": { "title": "Home page", "content": "
Hello World
\r\n" } } } ```
# GraphQL in short - Queries, mutations & fragments - Underfetching instead of overfetching
# Underfetching (1/2) ```js { products(search: "Hood") { items { id sku name meta_title meta_keyword } } } ```
# Underfetching (2/2) ```js { products(search: "Hood") { items { id sku name } } } ```
# GraphQL in short - Queries, mutations & fragments - Underfetching instead of overfetching - Multiple subqueries
# Multiple subqueries (1/2) ```js { products(search: "Hood") { items { categories { products { items { sku } } } } } } ```
# Multiple subqueries (2/2) ```js { products(search: "Hood") { items { sku } } cmsPage(id:2) { title content } } ```
# GraphQL - A flexible way - (developer-oriented, performant, easy-to-understand) - ... to fetch data - (assuming that the server-side API supports GraphQL) - ... and use these JSON data - (in client-side environments, based on whatever you want)
# GraphQL provides the neck in headless #### (Magento is the body, the frontend is the head)
# Magento in a few minutes
# Magento 2 in short (1/2) - Popular open source cart - Most popular today, maybe not tomorrow - Known for its modularity, extensibility & bizar complexity - Magento 1 and Magento 2 are different products - Magento 1 reaches End-Of-Life in June 2020 - Taken over by Adobe - Commercial product renamed to Adobe Commerce (Cloud) - Focused on enterprise, which is leading to market shift
# Magento 2 in short (2/2) - Magento PWA Studio development stack - Introduced with Magento 2.3 - Based on React, Redux, Apollo Client and GraphQL - Offering Buildpack, Peregrine, Venia Concept, Venia UI, UPWARD - Magento GraphQL API - Introduced Magento 2.3 - Complete coverage expected with Magento 2.4 - Extensible using Magento 2 extensions
# Sample custom query ```js { hello(name:"World") { name result } } ```
# Magento 2 module - `etc/module.xml` - `registration.php` - `composer.json` - `etc/schema.graphqls` - `Model/Resolver/Hello.php`
# Magento 2 `etc/schema.graphqls` ```js type Query { hello ( name: String @doc(description: "Who to greet?") ): Hello @resolver(class: "Yireo\\Example\\Model\\Resolver\\Hello") @doc(description: "Simple Hello World example") } type Hello @doc(description: "Hello answer") { name: String @doc(description: "Original input") result: String @doc(description: "Result") } ```
# Magento 2 Resolver class ```php namespace Yireo\Example\Model\Resolver; use /** **/; class Hello implements ResolverInterface { public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { return [ 'name' => $args['name'], 'result' => 'Hello, ' . ucfirst($args['name']) ]; } } ```
# Extending Magento 2 GraphQL - Add new endpoints - Extending the Store Configuration - Modifying / extending existing endpoints - Your own `etc/schema.graphqls` - DI interceptor plugins - Testing - Integration tests - API Functional tests
## Examples #### github.com/yireo/Yireo_AdditionalEndpointsGraphQl/ #### github.com/yireo-training/
# Client-side GraphQL
# GraphQL clients - GraphiQL - ChromeiQL, GraphiQL web-version, desktop apps
# GraphQL clients - GraphiQL - ChromeiQL, GraphiQL web-version, desktop apps - Shell - curl, GraphQL CLI
# GraphQL clients - GraphiQL - ChromeiQL, GraphiQL web-version, desktop apps - Shell - curl, GraphQL CLI - JavaScript - Axios, Apollo Client, Prisma, Apollo
# Usage of GraphQL - React - Magento PWA Studio, FrontCommerce, DEITY, React Storefront, Reaction - Vue - Vue Storefront - jQuery - And thus in original Magento 2 frontend with Knockout & Require - PHP client - Guzzle HttpClient, for instance in WordPress, Laravel or Symfony
# Some examples
# React example ```js import gql from 'graphql-tag'; import { useQuery } from '@apollo/react-hooks'; import PRODUCT_QUERY from 'queries/product_info.graphql'; const ProductContainer = ({ props }) => { const { loading, error, data } = useQuery(PRODUCT_QUERY, props); if (loading) return 'Loading...'; if (error) return `Error! ${error.message}`; return (
); } ```
## React connecting to Magento 2 using GraphQL JavaScript in browser (React) fetches data from Magento via AJAX call with GraphQL query
# Vue example ```js export default { apollo: { productPage() { return { query: PRODUCT_QUERY, variables() { return { id: this.productId }; } result (result) { this.updateProductPage(result.data.product); }}}}} ```
## Vue connecting to Magento 2 using GraphQL JavaScript in browser (Vue) fetches data from Magento via AJAX call with GraphQL query
# PHP example ```php $response = $this->client->post('/graphql', [ 'form_params' => [ 'query' => PRODUCT_QUERY, 'variables' => [ 'id' => $productId ] ] ]); echo $response->getBody()->getContents(); ```
## Laravel as a middleware Laravel fetches data from Magento using GraphQL and then processes this using PHP server-side rendering (Blade, Twig, etc)
## Headless means freedom of choice
# Freedom of choice - To make any frontend capable of consuming GraphQL APIs - To replace parts of Magento with other services - To replace Magento
# Beyond Magento
# E-commerce platforms supporting GQL
Magento
Native
CommerceTools
Native
BigCommerce
Native
Shopify
Native
Shopware
3rd party plugin
Sylius
No
WooCommerce
No
...
# CMS platforms supporting GQL
Contentful
Native
GraphCMS
Native
Strapi
Native
Prismic
Native
Gatsby
Native
WordPress
3rd party plugin
Drupal
3rd party plugin
...
## Simple Wordpress / Magento combo One client communicating with Magento via GraphQL and with WordPress also via GraphQL (or theoretically something else)
# Magento GraphQL query ```js { products(search: "Hood") { items { id sku name } } } ```
# WordPress GraphQL query ```js { posts { edges { node { id title date } } } } ```
Link: github.com/wp-graphql/wp-graphql
## Wordpress / Magento combo with middleware One client communicating with middleware via GraphQL and middleware communicating with Magento and WordPress also via GraphQL
# Combined GraphQL query ```js { products(search: "Hood") { items { id, sku, name } } posts { edges { node { id, title, date } } } } ```
# Other APIs supporting GQL - GitHub - GitLab - FaunaDB - ...
# APIs not supporting GQL - ElasticSearch - ...
## Magento in front of ElasticSearch WARNING: Do not put Magento in front of ElasticSearch, because ES performs extremely better than Magento
## ElasticSearch / Magento combo WARNING: Do not expose ElasticSearch directly to the internet!
## ElasticSearch / Magento with middleware Client fetches data from Magento using GraphQL and queries ElasticSearch via middleware
## ElasticSearch / Magento with middleware (2) Client fetches data from middleware that communicates with Magento and ElasticSearch
# Microservices
# Your own microservice - Pick a programming language - Pick a problem subset (service) - Write your own logic for this (using DDD, TDD?) - Wrap this in an GraphQL API
# Pick a programming language - Node - Apollo, Relay, Prisma - PHP - GraphQLite, Webonyx GraphQL - Or any language - Java, Ruby, Python, Go, ...
# Pick a service to add as microservice - ElasticSearch - Third party project: GraphQL proxy - Do not use Magento to wrap ElasticSearch because its slow - Product Information Management (PIM) - Handy in managing product information - Often slow in access - Enterprise Resource Planning (ERP) - Handy in managing all kinds of information - Often slow in access
# Or build your own microservice - Caching service - Price calculation - Alternative inventory & stock - VAT verification - Email verification - Dealers / retailer information - Complex product options - Promos and advertizing - Image optimization - Product image provider
## Example GraphQL-based infrastructure
# Introducing GraphQL Mesh
# GraphQL Mesh - Created by The Guild - Automatic discovery of backend API type - Swagger, OpenAPI, json-schema, SOAP, Federated GraphQL, etc - Turns it into plain GraphQL See: graphql-mesh.com
# Mesh or mess?
### Ask yourself: ## Is GraphQL required? Or is REST just fine?
# Why use GraphQL everywhere? - Consolidate API syntax - All systems offer the same API language (GraphQL) - Real headless frontends - No knowledge needed on backend service - Easier refactoring - Well, at least in theory
# Challenges with microservice management - Bundling multiple APIs into single API - GraphQL schema stitching, GraphQL schema proxies - Maintaining all APIs across multiple networks - Versioning, client-management, proxies - Performance - Caching (client-side, server-side), cache invalidation - Server Side Rendering
# Apollo - Apollo Client - **Apollo Server** - **Apollo Gateway** - Apollo Graph Manager - **Apollo Federation** See also principledgraphql.com
## Apollo Federation architecture
# Turn everything into GraphQL
# One API to rule them all