Business

How Facebook Made a Universal Open Source Language for the Web

The code that runs the web is a melting pot of programming languages and technologies. JavaScript, the most popular language on the web, is the standard for writing code that runs in your browser. But the server side is much more diverse. Java (no relationship to JavaScript) remains popular, as do PHP, Python, and Ruby. Mobile app developers, meanwhile, have their own preferred languages, like Kotlin for writing Android apps or Apple’s Swift for iOS.

The diversity of languages creates a need for a lingua franca that applications can use to talk to one another, regardless of the language used to create them. For example, the mobile version of a travel app might need to extract a flight schedule or upload changes to your profile on a server running software written in a different programming language. Meanwhile, application developers increasingly outsource parts of their software to cloud services that handle tasks such as sending text messages; the companies offering those services need to make them compatible with multiple programming languages.

Historically, that lingua franca has been something called REST, short for “representational state transfer,” a simple but sometimes blunt approach to sharing information between applications and servers. But a more flexible alternative called Graph Query Language, developed by Facebook, is spreading fast and has won over companies ranging from GitHub to Audi.

GraphQL is heavily inspired by another language called Facebook Query Language, which in turn was based on Structured Query Language, the well-established lingua franca of database software supported by Oracle, Microsoft, and other database makers. That makes the structure and syntax of GraphQL familiar to many developers.

GraphQL saw explosive growth among JavaScript developers over the past two years, according to data compiled by NPM, which offers tools for managing JavaScript code. The annual State of JavaScript survey conducted by a separate group of developers, which polled 20,000 JavaScript developers about the tools they use, found more than 20 percent already use GraphQL and another 62.5 percent want to learn it, up from 5 percent who had used it in 2016.

How It Works

Adam Neary, a tech lead at Airbnb, says the company chose to use GraphQL because of the limitations of REST.

With REST, you can only download predefined bundles of information. If you only want one part of that bundle, you still need to download the whole bundle and throw out the parts you don’t need. If you need a little information from each of several bundles, you have to download all the bundles and then reorganize it yourself.

For example, let’s say you’re a mobile developer at Airbnb and you want to add a feature to the iOS app that displays available properties in a particular zip code. In a REST system, you would request property listings by availability and locations, likely with the help of a “back-end” developer. But that could harvest a lot more information than you need, such as the number of bedrooms and bathrooms, the description, the list of amenities, the name of the host, and photos.

If you want your app to display only a small portion of this information, such as the headline and the number of bedrooms and bathrooms, then you either need to filter out the information you don’t need, or get the back-end developers to help you request a different bundle. If you want to make changes, such as adding the name of the host, that will means asking the back-end developers once again to make changes. But downloading unnecessary information will make your app slower and less responsive.

“The amount of over-fetching we were doing was insane,” Neary says.

GraphQL solves these problems by enabling the “front-end” developers who write code for mobile apps or web browsers to request specific information, without backend developers having to predict and define bundles ahead of time. Instead of sending a command to the server that says, essentially, “give me all the property listings available now in 90210” and then filtering out the unneeded information, you could send a command that says “give me the headlines and bedroom and bathroom counts for available properties in 90210.” If you later want to add the name of the host, you can do that without pestering the back-end engineer and without making changes that will break older versions of the app.

GraphQL has other benefits as well. It makes it possible to specify what type of data you expect to receive in response to a particular request. In other words, if you think what you’re asking for a zip code, you can make sure you’re getting numbers instead of words, and if you want a property photo, that you get an image file instead of text.

Solving a Problem at Facebook

Facebook didn’t set out to create a common language for other companies to use, explains GraphQL co-creator and former Facebook engineer Lee Byron. GraphQL’s origins date to 2012, Byron says, “when Facebook started taking mobile really seriously.” At the time, he says, Facebook’s mobile developers faced problems fetching different parts of user activity feeds.

Facebook didn’t release GraphQL to the public until 2015. “At first we weren’t sure that other companies would have the same kind of problems we had,” Byron says. But as he and other GraphQL developers talked to colleagues elsewhere, they realized that others faced the same problems, and had tried similar solutions. “When we explained GraphQL to them, their eyes lit up,” Byron says. “The said ‘that sounds better than what we have, we’d love to use that.'”

As often happens with open source software, a company has emerged to offer commercial support for GraphQL. Apollo, which spun out of a startup called Meteor in 2016, makes a product designed to make it easier for developers to build and maintain services that support GraphQL, along with features designed to boost performance.

Last month Facebook announced that it would establish an independent organization under the umbrella of the Linux Foundation to own and manage GraphQL. Byron credits the decision to the growing number of people, himself included, outside of Facebook involved in not just using GraphQL but in developing it as well. By establishing an independent organization that owns the project, all the players in the GraphQL ecosystem can make sure that they can talk to with each other.


More Great WIRED Stories

Products You May Like

Articles You May Like

OpenAI’s ChatGPT Breaks Out of Its Box—and Onto a Canvas
Google’s Visual Search Can Now Answer Even More Complex Questions
Hurricane Helene Destroyed Roads. Here’s How to Rebuild—and Flood-Proof Them for Next Time
AI’s Big Gift to Society Is … Pithy Summaries?
FTX Customers Will Get Back Billions After Judge OKs Bankruptcy Plan

Leave a Reply