GraphQL is an open-source data query language for APIs and a runtime for fulfilling those queries with your existing data. It provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015. The GraphQL project was moved from Facebook to the newly-established GraphQL Foundation, hosted by the non-profit Linux Foundation on 7 November 2018.
GraphQL has an efficient, powerful and flexible approach to developing web APIs. It queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.
GraphQL supports reading, writing (mutating) and subscribing to changes to data with real-time updates.
With GraphQL, the user is able to make a single call to fetch the required information rather than to construct several REST requests to fetch the same.
Features of GraphQL
Defines a data shape
GraphQL queries mirror their response. This makes it easy to predict the shape of the data returned from a query, as well as to write a query if you know the data your app needs. This makes GraphQL really easy to learn and use. It's unapologetically driven by the data requirements of products and of the designers and developers who build them.
Another important aspect of GraphQL is its hierarchical nature. It naturally follows relationships between objects, where a RESTful service may require multiple round-trips or a complex join statement in SQL. This data hierarchy pairs well with graph-structured data stores and ultimately with the hierarchical user interfaces, it’s used within.
A GraphQL server can be queried for the types it supports. This creates a powerful platform for tools and client software to build atop this information like code generation in statically typed languages, Relay, or IDEs like GraphiQL. GraphiQL helps developers learn and explore an API quickly without grepping the codebase or wrangling with cURL.
Each GraphQL field on the server is backed by any arbitrary function. It had to leverage all this existing work to be useful, and so does not dictate or provide any backing storage. Instead, GraphQL takes advantage of your existing code.
Each level of a GraphQL query corresponds to a particular type, and each type describes a set of available fields. Similar to SQL, this allows GraphQL to provide descriptive error messages before executing a query.
The shape of the returned data is determined entirely by the client’s query, so servers become simpler and easy to generalize. When you’re adding new product features, additional fields can be added to the server, leaving existing clients unaffected. When you’re sunsetting older features, the corresponding server fields can be deprecated but continue to function. This gradual, backward-compatible process removes the need for an incrementing version number.
GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.
Another major use case for GraphQL is the integration of multiple existing systems behind a single, coherent GraphQL API. This is particularly compelling for companies with legacy infrastructures and many different APIs that have grown over the years and now impose a high maintenance burden. One major problem with these legacy systems is that they make it practically impossible to build innovative products that need access to multiple systems. In that context, GraphQL can be used to unify these existing systems and hide their complexity behind a nice GraphQL API. This way, new client applications can be developed that simply talk to the GraphQL server to fetch the data they need.
GraphQL is particularly great for front-end developers since it completely eliminates many of the inconveniences and shortcomings that are experienced with REST APIs, such as over- and under fetching. Complexity is pushed to the server-side where powerful machines can take care of the heavy computation work.
GraphQL embraces modern trends on how applications are built. You may only have one backend application, but multiple clients on the web, phones, and smartwatches depending on its data. GraphQL can be used to connect both worlds, but also to fulfill the requirements of each client application - network usage requirements, nested relationships of data, fetching only the required data - without a dedicated API for each client.