What is GraphQL and How Does it Work
GraphQL is an open-source data technology developed and released by Facebook in 2015. The goal of GraphQL is to save bandwidth and increase the responsiveness of user-facing interfaces by simplifying communication and data transfer methods. It achieves this by acting as a middleman between end-user clients and data sources. GraphQL can also be called a communication pattern.
History and name
As stated on the official GraphQL website, it was in 2012 that Facebook started using GraphQL with its iOS and Android mobile applications before releasing it to the public in 2015 as a specification under the MIT license.
It's not known if it was named GraphQL at the time but the two parts that together create the compound name - "Graph" and "QL" (Query Language) - help paint the picture as to why GraphQL was developed.
When thinking about a graph, most people will imagine a two-dimensional X and Y-axis representation of data values for one or more variables. In the case of GraphQL, the graph is representative of the Social Graph.
Facebook coined the term "social graph" as a representation of information on its social network. A social graph is a lot more complex than a regular one. It presents a myriad of variables and the relationships between them that anyone user or entity on the Facebook network would have.
The "QL" or query language represents just that: a language for querying (asking) the social graph for specific information about one or more entities. This is the forte of GraphQL. The ability to ask for and receive custom information using a single request as opposed to multiple. Apps that use GraphQL display information quickly even on slow Internet connections because of this. No more multi-requests just for a screen of info.
How does GraphQL Work?
At its core, GraphQL is just a specification. A set of instructions was released by Facebook on how it works and how it could and should be implemented. The official description is:
"GraphQL is a query language and execution engine tied to any backend service."
To use GraphQL, a server that implements the specification is needed. Because GraphQL is language agnostic, there are many implementations in almost all popular programming languages for both servers and clients.
After the server is installed, it's time for configurations. A configuration includes creating a "schema" or structure/description of the data being served and multiple "resolvers" or functions that retrieve that data.
Besides being language agnostic, GraphQL is also data source agnostic. This means that GraphQL can connect to almost any type of data source (API, database, flat-files, etc.) to collect necessary data.
Due to the similarities in wording like "schema" and "QL", GraphQL is often mischaracterized as a database language like SQL (structured query language). This is a common misconception because, in reality, GraphQL does not store any final persistent data besides the "schema" description.
Once fully configured, the GraphQL server is ready to respond to queries just like a regular API endpoint would. The difference is that one GraphQL endpoint can retrieve and respond with a multitude of data types and entities from a range of data sources, limited only by the initial setup. Compare this to a regular API endpoint, say "/products" which would only be providing information on only one type of data entity.
Pros & Cons of using GraphQL
The advantages of using GraphQL heavily outweigh the disadvantages when considering the end-user experience. Apps using GraphQL are significantly faster in displaying data. In many cases, they also use a lot less data traffic.
One of the biggest advantages that GraphQL has over traditional methods of retrieving data (REST APIs) is its efficiency when dealing with both over and under fetching of data. Because GraphQL can construct a response from multiple sources or multiple requests, the client (end-user) doesn't have to anymore. GraphQL can also limit responses to contain only the requested fields, eliminating thus the high bandwidth burden that comes with over-fetching of information.
The downside of using GraphQL comes in the form of computing power consumption and traffic at the source. Because the client can get away with making only one request, it's left on the server to build that response with the corresponding number of compound requests.
When to Implement GraphQL
Typically, the advantages of using GraphQL grow exponentially with the complexity of the data sources. An online shop that stores information about multiple entities like clients, products, sales, markets, and prices will benefit a lot more from a compound query as opposed to say an analytics service that stores data in just one table (entity).
With this in mind, it becomes clear now why Facebook developed GraphQL. Being the largest social network in the world, it also maintains the largest social graph. We can only guesstimate how many entities make up that behemoth of data and all the relationships between them.
As more and more people from around the world started using Facebook, not everyone benefited from the same technology advancements like 4G mobile data speeds or the latest processors. Not to mention data caps and limits that are widely and wildly different in many markets. GraphQL enabled Facebook to provide a consistent and efficient experience for everyone.
GraphQL has become a great solution for providing consistent and efficient experiences for end user-facing services that contain complex data. Twitter, GitHub, PayPal, Lyft, and even Starbucks are just a handful of the global companies that are currently using GraphQL.
If your company is already offering access to complex data and services via APIs, GraphQL is the next logical step in upgrading the experience for your end-users.
Does adding a new front-facing service make you anxious? Don't be! GraphQL can sit behind any of your current API endpoints or a new one. This way, all your current access controls, and security measures remain intact. It's easy to get started with GraphQL.