DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • GraphQL vs REST — Which Is Better?
  • Designing Scalable Java APIs With GraphQL
  • When It’s Time to Give REST a Rest

Trending

  • The ORM Is Over: AI-Written SQL Is the New Data Access Layer
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • Evaluating SOC Effectiveness Using Detection Coverage and Response Metrics
  • You Don't Get to Retrofit Trust: Why API Security Must Be Designed In, Not Bolted On
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. GraphQL Benefits in a REST API, But How?

GraphQL Benefits in a REST API, But How?

Curious as to how GraphQL compares to RESTful services? We take a look at some key differences and what GraphQL is trying to deliver.

By 
Ross Garrett user avatar
Ross Garrett
·
Aug. 14, 17 · Analysis
Likes (8)
Comment
Save
Tweet
Share
16.8K Views

Join the DZone community and get the full member experience.

Join For Free

GraphQL is an open sourced API specification from Facebook that is touted as the next evolution from RESTful APIs.  The primary benefits at a high level is a reduction in how many times an endpoint (or multiple endpoints) are called to get only the information that is requested.  In doing so, solves the problem of over and under fetching in the JSON payload.  Hold the pickles, thank you.

The primary driver of this specification was to better navigate dodgy network speeds for mobile applications.  GraphQL APIs allows you to only fetch the information you need, thereby preserving bandwidth that might otherwise be taken by larger payloads.  At almost 621MB in size with both the Facebook and Facebook Messenger apps, there is already a strong pull on the user’s device (Aug. 2017 iOS).

GraphQL is a query language (hence the QL) built on a schema to only query specific points in the data structure, compared to the entire JSON payload of a REST API call.  This can be very beneficial in use cases noted above like mobile where only certain parts of the application’s UI need to be updated to increase overall speed.  GraphQL is often used alongside with React & Redux on the front end to serve up specific new components when needed.  

Let’s take a look at the difference in the calls:

calls.jpg

Image credit: howtographql.com.

Making this request is a query, similar to a GET call.  Modifying the data as part of the Create, Retrieve, Update  and Delete pattern (CRUD) is called a mutation.  All of your queries and mutations are set within your GraphQL schema for the API.  When looking to build out your own schema here is a great cheat sheet for the schema language and its types.


Another key benefit is the ability to combine what would be separate POST & GET calls into one, because of the deep nesting built into a schema.  For example, you are able to create a new user and assign an id attribute to that user, as a piece of that new user’s data.  Then receive the updated unique id from the server for that user in the response payload.  All of that is completed in one request-response round trip.   Beam me up Scotty!

graphql-id.png

Image credit: howtographql.com.

But what about incoming events...like from the internet?

Enter subscriptions.  Subscriptions keep a direct line to the API so when events occur they are streamed to the client.  Subscriptions are however very new and were just recently merged in March as a part of the overall GraphQL specification.  We will likely see subscriptions emerge as changes to current webhooks so they act closer to streams.

This is glorious! Throw out the REST APIs! Viva la GraphQL! (“Graph, Graph, Graph” echoing chants in the background)

What does this mean for Cloud Elements and our 100% RESTful API Platform?  Well the short answer is we kind of already solve this problem, using the REST you already know.  Let me explain.

The vast majority of large scale APIs are RESTful, serving up structured JSON payloads.  This creates an enormous amount of consistency in both what the provider can test for in sending and what the client expects to receive.  But even with this consistency, not every API is built the same, in fact far from it.  Some support webhooks, but a majority are still using polling.  Even then, 15% of all public APIs are still SOAP, the API structure that REST was supposed to kill off.  

Each cloud application (or Element) in our catalog is structured with our own query language (CEQL).   This allows us to make a normalized one-to-many API call across different Elements (our Hubs) and combine the payloads.  For example, the GET: /hubs/crm/contacts call will return the same contact structure across Salesforce, Sugar, ZOHO and Microsoft Dynamics (p.s. Microsoft Dynamics is actually a SOAP SDK...oh dear).

Great, but that doesn’t explain how you solve the over-under fetching?

The magic comes with our Transformations, the ability to map the data fields in each of the Elements to a customized data structure.  Now imagine you only want to GET a unique contact’s email, phone number and zip code from ZOHO and POST it to HubSpot? Our transformations pull the full JSON payload, strips everything but the requested data and only transmits the needed fields into the Element you want.

full-json.png

transformed.png

Finally, GraphQL itself is only a specification, similar to the Open API Initiative specification.  In order to actually build and implement your new GraphQL based API there are vendors like graph.cool alongside a whole community of plugins, servers, and even a CMS.  

There is a strong shift in adopting GraphQL by digital content providers like Twitter, Pinterest, and even the Financial Times.  Therefore we will continue to support the API community as the progression towards newer specifications becomes more mature.  Regardless of the endpoint, we will power your integration strategy, sign up for a developer account to play with your own transformations.

API REST Web Protocols GraphQL

Published at DZone with permission of Ross Garrett. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • GraphQL vs REST — Which Is Better?
  • Designing Scalable Java APIs With GraphQL
  • When It’s Time to Give REST a Rest

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook