The Insider’s Guide to GraphQL
Creating APIs has always been a tedious process. With REST, API architectures consist of several endpoints to serve data and make changes, but quickly can sprawl out of control. Even with good documentation, bridging the gap between the server and the client can be a struggle.
With the release of GraphQL in 2015, setting up and using your API has never been easier. With it, you can consolidate your API into a single endpoint with a robust, language-agnostic query language that makes API development a breeze.
GraphQL was designed to fix some of the glaring issues with API architecture for the web and the explosion of mobile apps. Understand how GraphQL works, the advantages it brings over REST APIs, and see how it can improve your tech stack. Plus, we’ll show you how Devii can take GraphQL even further!
What is GraphQL?
In the days of the late ’00s, Facebook had seen that the mobile world was growing. iPhones were quickly becoming popular, Android was on the way, and the world looked like it was shifting to the web. With this, Facebook decided to go all-in with HTML 5, so their mobile site would run smoothly on any browser.
Of course, mobile browsing quickly became mostly through proprietary apps, and built-in browsers were sluggish and unoptimized on both platforms.
Because their gamble on HTML was failing them, they had to resort to something new. They began work on a way to make their app (which was just a wrapper for their mobile site) faster and more reliable, but a RESTful API wasn’t working for them. They were too sluggish for users running on 3G data and tedious to maintain as their platform grew.
In 2012, Facebook created GraphQL, a new query language that would solve all their problems with RESTful APIs. It was faster, no longer had to deal with over or under-fetching data, and allowed them to visualize data as graph tables instead of URLs.
After being in use for a few years, and the recent announcement of Relay (an application framework built atop their React framework and GraphQL), Facebook decided to open source the GraphQL spec to the world. GraphQL eventually separated from the company to join the Linux Foundation and form an independent entity known as the GraphQL Foundation.
What Makes Up GraphQL?
GraphQL comes with many individual elements that make up the query language, as well as some built-in features. Let’s cover some of the essential elements:
- Fields: At a basic level, GraphQL is asking about different objects or the values of specific fields. They are parts of the object you’re requesting, each with its own values.
- Arguments: Unlike RESTful APIs, you can pass arguments within your GraphQL queries. These are the query parameters that you send in your request to get back the correct response.
- Fragments: Need to re-use a query later on in your code? These allow you to construct sets of fields to be used across different queries when needed.
- Mutations: It’s an API, so you’ve gotta have a way to modify server-side data. Technically, with GraphQL, any request could potentially write data, but the established convention for this is with mutations.
- Schemas: These describe the shape of the data your API makes available. It has a hierarchy of types with fields from your backend. They also define a collection of types and the relationships between objects.
- Types: This is the individual data itself. Strings, integers, and objects are all types that GraphQL stores.
- Resolvers: Whenever a field is included in a query, a function called a resolver runs to produce the corresponding value. These must be provided by the developers and can handle multiple queries in one call. Because creating these resolvers can be tedious, Devii generates these automatically, thanks to its Auto Resolvers.
How does GraphQL work?
GraphQL operates as two parts: A query language to send queries to your API, and a server-side runtime that processes these requests. Instead of looking at your data as a resource, GraphQL APIs look at them as if they’re a graph. This allows developers to better visualize how their data is constructed, and define your schema and queries to make the requests you need.
Unlike RESTful APIs, where there are multiple endpoints that serve different data, GraphQL offers a single endpoint that does it all. Clients can make requests with the GraphQL query language, fetching or mutating data, and optimizing their requests without worrying about over/under fetching.
On the server side, the GraphQL runtime also has two parts, schemas and resolvers. The schema defines what your API looks like, including the available data types, fields and operations. Resolvers, on the other hand, are functions that will specify how each request will return data to the client.
GraphQL with SQL vs NoSQL
SQL's query language closely resembles the way databases are structured and is designed to express the user's desire. Cassandra, MongoDB, and Firestore are examples of NoSQL databases that have their own query languages. These query languages are tied to how data is stored and efficiently fetched. SQL may look a lot like other languages, but it cannot express the richness of the data stored in these systems since SQL is simply not powerful enough. It is true that all databases work; however, they are accompanied by query languages that are specific to the implementation.
In an enterprise with a variety of databases, understanding syntax and optimizing each is a cognitive strain.
GraphQL is a natural language for accessing and manipulating the data for both SQL and No SQL.
While GraphQL may be a query language, it only queries your API, not your database. Because of this, it’s got the flexibility to handle both SQL and NoSQL databases easily. With resolvers powering the GraphQL server, they can handle most popular database architecture. Heck, you can use GraphQL with no database at all if you need to.
Despite GraphQL working with SQL and NoSQL, an application middleware that speaks GraphQL at the top and the native SQL and/or NoSQL language at the bottom is needed to truly complete the solution.
The Advantages of GraphQL
GraphQL has been a rising presence in the tech world. Since 2018, it’s grown exponentially and currently is almost at 10 million downloads on NPM alone. Here are some of the most significant advantages it has to offer:
- It’s fast. Each query describes precisely what the client needs so that the backend can send the response in a single call. No more over/under fetching data.
- It’s robust. A GraphQL API will define what’s possible with static types, while clients will request the data they need with queries, which are selections of those types. Because of this, queries get verified before being implemented on either side.
- It’s empowering. Because a client will request exactly what they need, their request can be fulfilled without waiting on server-side changes.
GraphQL vs. Rest API
We all know about REST, the current king of API architecture. While its reign has been strong over the last 20 years, powering most modern technology, it’s not perfect by any means. It can be sluggish, it’s strict in the data it serves to endpoints, and there are so many endpoints.
However, it’s not the only lion in the jungle. (Also, lions don’t live in jungles. Why does everyone think they do?)
GraphQL was created for a specific reason: because RESTful APIs weren’t good enough.
In a RESTful API, clients requesting data might need to call data from two or three endpoints to get all the data they need, aka under fetching. Instead, GraphQL offers a single endpoint for all of your requests and gives you the data your request exactly how you need it.
GraphQL also offers a more flexible way to develop your codebase. With REST, you need to clearly define each endpoint and what they can send or receive, which can be time-consuming. However, once a GraphQL API has been established, the client side only needs to create their queries to get what they need. There isn’t any need for complex API development to get the data you need.
Build, Test, and Deploy in Under an Hour with Devii
Devii ❤️’s GraphQL. Heck, it’s why we created Devii in the first place. We wanted to show the world the power of GraphQL and make API development a breeze. With our platform, you can accelerate your API development and take that process from days to just minutes. Here’s what we do to bring a GraphQL API from great to excellent:
Thanks to our Instant API Engine, you can spin up a working GraphQL API and start fetching data on day one. It’s designed to connect to your existing database, automatically generate GraphQL schemas, and get your data into the hands of whoever needs it. Spend less time worrying about your backend, and focus on features to improve your products and services.
With Auto Resolvers, Devii eliminates the hassle of long development times. Skip the lengthy development times of handwriting queries, types, and resolvers and get your frontend developers the resources they need to get the job done. Plus, your teams can deploy faster while Devii does all the heavy lifting.
And if there are other functions that you need coverage for, Devii offers packages to expand your API. We've got a package for you for geospatial data, file storage, and off-database keys. Save time with our built-in packages, and get back to innovating.
GraphQL has been a strong contender in the tech world for some time now. With its numbers growing every year, it’s definitely here to stay. The power and flexibility of a GraphQL API are hard to understate. It offers tons of benefits over your standard RESTful API, and it’s never been easier to dive into.
Stop burning development hours on GraphQL API development. Get started with a Devii demo today.