Compares how GraphQL and the JSON REST API standard fits for e-commerce and CMS applications
GraphQL vs. JSON:API
View Slide
AimeosE-Commerce framework
GraphQL
Invented by Facebook
Retrieve graph data efficiently
For complex queries
Minimize response data
{product {namemedia {url}}}Queries
{"data": {"product": {"name": "Simple product","media": [{"url": "http://localhost/img/simple1.jpg"},{"url": "http://localhost/img/simple2.jpg"}]}}}Response
Advantages
One API for allclient requirements
Efficient requests includingrelated resources
Retrieve only the datayou really need
Writing several (related)resources at once
Exact schema definitionprovided by the server
type Product {id: IDlabel: Stringstatus: Boolean}Schema
Well defined query language
{product(code: '1000.001') {labelattribute(type: 'variant') {idname}}Query
Many client sidelibraries available
Disvantages
No caching by CDNsor proxies possible
Optimizing queriescan be hard
Clients can build deepand complex queries
Client and server arethighly coupled
All application state must bemaintained by the client
Query language addsmore complexity
curl -X POST -H 'Content-Type: application/json'-d '{ "query": "{ products { name } }" }'http://localhost/graphqlQuery
Client side caching necessarymay introduce additional points of failure
{ json:api }
Invented by Ember.js
Reduce the amountof client code
Easy client andserver code
Use best practices
curl -H 'Content-Type: application/json'http://localhost/jsonapi/productQuery
{"data": [{"id": 1,"type": "product","attributes": {"uid": "1","code": "SP01","name": "Simple product",}}]}Response
Efficient requests includingrelated resourceshttp://localhost/jsonapi/product?include=price
{"data": [{"id": 1, "type": "product","attributes": { … }"relationships": {"price": [{"id": "123"}]}}],"included": [{"id": 123, "type": "price","attributes": { … }}]}Response
Retrieve only the datayou really needhttp://localhost/jsonapi/product?fields=id,name
Caching byCDNs, proxies and browsers
Client and serverare decoupled
Application statemanaged by the server
Hypermedia As The Engine Of Application State"links": {"self": "http://localhost/jsonapi/product/13","next": "http://localhost/jsonapi/product/21","basket/product": "http://localhost/jsonapi/basket/default/product"}HATEOAS
Queries can be optimizedfor the required use cases
API interactionis plain simple
Only single resourcecan be updated at once
No specification of thequery language
No schema returnedby the server
No automaticschema validation
Conclusion
Best for internal APIs
Perfect as single endpointfor clients
Lacks of scalability
Clients and servers canevolve independently
Can scale infinitely
Not for write heavy usage
JSON:APIKeep for the frontend
GraphQLUse for the admin backend
Aimeosaimeos.orgTwitter: @aimeosfacebook.com/Aimeos