Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
The API Journey: from REST to GraphQL
1. The API Journey:
from REST to GraphQL
Haci M. Yaman
19/4/2021
GraphQL
XML-RPC
JSON-RPC
2. Content
1. RESTful Microservices
2. The Need for Better
3. GraphQL
• Introduction
• History
• Example
4. Apollo GraphQL
• Server
• Federation
5. The End
• References
3. 1. RESTful Microservices
• REST = Representational State Transfer Technology
famously using JSON (but can use XML and YAML)
• Tightly coupled with HTTP protocol
• URLs = meaningful Resources like
Directories or Tables of data
• Famous Pet Store
See: https://petstore.swagger.io/
• Rival was/is
SOAP = Simple Object Access Protocol
using XML
4. 2. The Need for Better
• RESTful resource always returns same data structure
• e.g. GET /customers/1 { id, firstName, lastName, birthDate, gender, … }
• but one app may need one subset of attributes, another may need different subset
• Hierarchical resources need to many calls to collect data needed:
• (A) customer details GET /customers/1
• (B) account list GET /customers/1/accounts
• (C) recent transactions GET /customers/1/accounts/101/transactions?limit=3
• (D) recent transactions GET /customers/1/accounts/102/transactions?limit=3
• (E) recent transactions GET /customers/1/accounts/103/transactions?limit=3
• (A) and (B) can be executed in parallel; (C), (D), (E) can be executed in parallel but response for (B) is required first
• Many calls on Server-to-Server are much faster than, Client-to-Server
• Working with many RESTful microservices is not easy
• One-way communication: from client to server; server cannot send messages to client
• Custom end-points are required for each client to make it efficient
• Documents can get out of date easily like OpenAPI YAML files
• Analytics is poor; no fine-grained usage info
5. 3. GraphQL (a)
• Uses only one end-point
• As simple as HTTP POST {host}/graphql
• Easy to understand data definition (schema):
• “Describe what is possible with a type system”
• Less parsing; More stable; Dev-friendly errors
• “Ask for what you need, and get exactly that”
• Faster communication; Less hosting cost
• “Get many resources in a single request”
• Faster communication
• Schema file is the contract + documentation
• It can’t get out-of-date;
• Easy to develop + debug via IDE integration
• Subscribe to changes on a query;
server will inform you
• Ref: https://graphql.org/
6. 3. GraphQL (b)
• Facebook has been using GraphQL since 2012
• Public Release in 2015
• A Query Language for APIs
like SQL for Databases
• Model your business domain
as a “Graph”
• See https://apis.guru/graphql-voyager/
8. 4. Apollo GraphQL (Server)
• “… GraphQL server that is compatible with any GraphQL client …”
• Easy way to create a GraphQL server
• See https://www.apollographql.com/docs/apollo-server/
9. 4. Apollo GraphQL (Federation)
• “To get the most out of GraphQL, you should expose a single data
graph … It can be challenging to represent an enterprise-scale data
graph with a single, monolithic GraphQL server.”
• Federated GraphQL servers are similar to many microservices;
easier to develop
• Apollo Gateway lets app
developers use a giant Graph
• See apollographql.com/docs/federation
10. The End
Thank you
API https://en.wikipedia.org/wiki/API
1991 CORBA https://en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture
1993 RDA https://en.wikipedia.org/wiki/Resource_Description_and_Access
1998 XML-RPC https://en.wikipedia.org/wiki/XML-RPC
1999 SOAP https://en.wikipedia.org/wiki/SOAP
2000 REST https://en.wikipedia.org/wiki/Representational_state_transfer
2005 JSON-RPC https://en.wikipedia.org/wiki/JSON-RPC
2007 OData https://en.wikipedia.org/wiki/Open_Data_Protocol
2012 GraphQL https://en.wikipedia.org/wiki/GraphQL
2016 gRPC https://en.wikipedia.org/wiki/GRPC
References: