GraphQL: A Fundamental Lesson for Beginners

By on May 22, 2019

graphql fundamental

In 2000, Roy Fielding, a computer scientist had introduced the very first idea of REST API to the world. This software architectural style was then developed to help developers encounter many obstacles such as accessing and using API. Not so long after that, REST became popular thanks to its various benefits.

Nevertheless, there are still some huge downsides when it comes to REST API.

For example, REST will use one URL for a certain resource. So, we have 2 scenarios:

  1. The request is simple. In this case, REST works and reduces the output perfectly.
  2. But when the request is complicated or comes with a massive amount, the number of the query in REST has to increase. Consequently, it takes much more effort and requests to resolve those queries. And nobody is happy with it.

In other words, REST can return a customized set of data but we need to strictly follow some conventions to do so. Meanwhile, returning a customized set of data is the default feature of GraphQL.

Consequently, a modern problem needs a modern solution, and GraphQL came up to rescue us developers.

So what the heck is GraphQL?

graphql for beginners

What is GraphQL? Image: Contentful.

GraphQL is another API design structure but its approach is way different from REST. This query language is inserted in the header of an API’s page and lets developers describe all the sent and received data. This also is a server-side runtime for fulfilling queries with the existing data.

GraphQL was developed and used internally by Facebook back in 2012 and released as an open source in 2015.

Here a simple example of how GraphQL works in reality by Sacha Greif.

Using REST is like embracing an old way to order pizza, get groceries delivered and call the dry cleaner to get the clothes. You have to make three phone calls to contact those 3 service providers.

But when it comes to GraphQL or in this example, your assistant, all you need is to give them your requests and information and they will do it for you.

And Greif ended his example with a conclusion: “In other words, GraphQL establishes a standard language for talking to this magical personal assistant.”

Thanks to its convenient and flexible way of approach, many big and small organizations like Facebook (of course!), Github, Amazon, etc. are using this new-born query language for their public APIs.

What could GraphQL do?

According to Scotch.io, these are 4 main features of GraphQL:

Hierarchical – queries look exactly like the data they return.

Client-specified queries – the client has the liberty to dictate what to fetch from the server.

Strongly typed – you can validate a query syntactically and within the GraphQL type system before execution. This also helps leverage powerful tools that improve the development experience, such as GraphQL.

Introspective – you can query the type system using the GraphQL syntax itself. This is great for parsing incoming data into strongly-typed interfaces, and not having to deal with parsing and manually transforming JSON into objects.

Then what are benefits when applying GraphQL to your project?

First of all, when using GraphQL, the amount of the client’s request is reduced. This way, you can save tons of time and effort spent on making requests. We only need one URL for all data fetching and mutating when working on a GraphQL server.

Furthermore, this query language is able to detect sent data’s form and receive detailed information of the required data as well as errors from APIs via its system.

What’s more? Well, GraphQL can automatically produce several basic forms of code when developers are writing their requests.

And because this API design structure is produced by types and fields, first, it can fetch data for different endpoints and second, access every possibility of the data from just one endpoint. As a result, clients will receive the exact data that they asked for in the beginning. This feature is what makes GraphQL an outstanding query language.

Some other advanced benefits when using GraphQL instead of REST or other query languages are:

– Being self-documented.

– Supporting evolving APIs over time.

– Supporting real-time functionality.

However, advantages come with downsides.

First, in terms of caching, in REST, the URLs are globally unique identifiers for the resources => client can use this to build a cache. In GraphQL, however, there’s no URL-like primitive for a given resource.

Second, if your project is a small one, there’s no need to use GraphQL to perform fetching tasks because this language is kinda complex. It’s like using a sledgehammer to crack a nut.

Last but not least, if you want to use GraphQL, prepare for the learning curve. Developers have to own the knowledge of Schema Definition Language, get to know new definitions (for example resolver, schema, etc.). If your team has to accomplish a project within a short while and limited resources, then the old friend REST will be more practical.

An example from Paypal

graphql paypal
Back in 2014, Paypal – one of the largest online payment systems in the world – still applied REST to design and implement. But the obstacle here was that REST’s principles underestimated the needs of web and mobile apps and their users. The issue makes it hard to perform an optimized transaction like a checkout.

This happened because Paypal’s developers had to make many roundtrips from the client to the server to fetch data when using REST APIs in their applications and this consumed a lot of time (at least 700ms/roundtrip). And each roundtrip makes the rendering time slower. The problem led to user frustration and lower checkout conversion.

So they decided to get rid of REST and turned to GraphQL. At first, they tried to implement it in a new product that needed integrating Paypal checkout into their app.

The first benefit was that developers didn’t have to discover which internal API to call, how to call it and transform that data into something else. Furthermore, with GraphQL, API developers know exactly what fields their clients are using and are able to make better choices when deprecating or evolving their API.

It resulted in a quick app, a productive team of developers and happy users.

After that, Paypal started to use GraphQL to work with API in every other project.


We hope that this article is helpful to you and your team. If you have any idea for your future software and want to apply this query language to that project, contact us now! We at Designveloper have used GraphQL since the first version was released and are familiar with it than anyone else.

1 Comment

Leave Comment

About Author

author

Subscription

Statistics

  • Today Visitor: 121
  • Month Visit: 59,430
  • Total Posts: 121

LikeBox

Facebook By Weblizar Powered By Weblizar