GraphQL: What Is a GraphQL API & How Does It Work?

GraphQL is an architectural style for delivering data- and content-focused application programming interfaces (APIs) using HTTP. Borrowing a little from both SOAP and REST, GraphQL uses HTTP POST methods to submit queries via an HTTP request’s body or query parameters. With the “QL” meaning “query language,” GraphQL provides a consistent query layer for APIs, providing a single endpoint for developers to use when making requests. This allows you to not only query the data, but also control the structure of how each GraphQL API responds. (Learn more fundamentals by reading our What Is a SOAP API? and What Is a REST API? posts.)

GraphQL provides wider access across a large number of schema objects and properties, making for a more flexible and powerful way to make digital resources available via web APIs.

Rather than designing individual API paths for each underlying resource, GraphQL gives you a single URL to query the data you desire as a consumer. Much like structured query language (SQL), and other database query languages, GraphQL provides a language for making requests of the data, while also describing the structure of the data being returned. Similar to other web API styles and protocols, you can make a request to a GraphQL API using the API’s URL:

When using GraphQL, you can choose to either use HTTP GET or HTTP POST to send your query. If you are sending a query via the body of your HTTP POST method, it might look something like this:

{
 __type(name: “Book”) {
  name
 }
}

This query is designed to search the data but also define what the structure of the data should look like, instructing the API to return just the name of all entries named “book”:

{
"data": {
 "__type": {
  "name": "Book"
  }
 }
}

GraphQL gives API providers a standardized way to deploy an API on top of a database, and gives API consumers robust querying capabilities using common web infrastructure. GraphQL only uses what it needs from the HTTP protocol, while heavily investing in a standardized message format over leveraging what already existed within HTTP. Consumers can get up and running pulling the data they need in a consistent and scalable way. Using the web to make data and content accessible, GraphQL brings forward many of the database capabilities developers are used to on the backend to give them the ability to rapidly build web, mobile, single page, and other types of applications.

Graphs instead of resources

GraphQL is all about seeing your data as a graph, and then querying that graph. It allows you to precisely define the schema of the data you are making available, then gives you a powerful query interface to navigate, traverse, and discover what you need. GraphQL gives you a strong framework to think deeply about each of the objects you are making available, then helps you define the relationships between these data objects, as well as additional iterations or variances of each object. You can better manage the data and how it is all related, while also getting help dealing with change across that data over time. GraphQL goes beyond just being a database, or a set of data resources, and empowers you to look at it all as a single entity that can be queried and traversed based upon how it is all connected.

APIs for web and mobile applications

GraphQL excels at delivering exactly the data that frontend developers of modern web and mobile applications need. GraphQL is an open source project developed at Facebook, as well as the popular single-page application platform React. Providing a powerful example of how APIs are being leveraged for the next generation of web and mobile interfaces, GraphQL APIs can significantly speed up the development of frontend applications by giving more control to the developer when it comes to querying a database for data and content. It gets out of the frontend developer’s way, giving them the ability to optimize each API request for a specific requirement of their multi-purpose applications. GraphQL provides an effective architectural style for rapidly delivering robust APIs, but also speeds up the development of the applications that will be putting these API to work.

A query language for APIs

Platforms like Facebook, GitHub, and Pinterest use GraphQL to help them not only make their massive amounts of data available to their consumers, but do it in a way that empowers frontend developers to be more agile and precise in how they deliver data and content to web and mobile applications. This provides a robust way to query data using a single HTTP URL, allowing developers to completely shape the structure of each API request and optimize the data that is returned. Developers can create, read, update, and delete data across large graphs of interconnected data that have all of the relationships defined and mapped out as a graph.

GraphQL APIs put the power and control in the hands of the API consumer—giving them exactly what they need, in the format they need it in. GraphQL uses the web as a transport but relies on its query language to negotiate what is being sent and received. Employing GraphQL, next-generation UI frameworks like React, Gatsby, and others assume modern application developers will be API-first, writing web, mobile, and single-page applications to GraphQL APIs. Ultimately, GraphQL provides a pretty powerful data view of the enterprise API landscape, giving API providers another powerful tool when it comes to making digital assets available via the web.

The future of GraphQL

Hear from industry experts about their thoughts and predictions around GraphQL, and watch a companion GraphQL panel-discussion video here.

Watch and learn more

Want to see how you can work with GraphQL in Postman? Watch this demo video:


What do you think about this topic? Tell us in a comment below.

Comments

Your email address will not be published. Required fields are marked *



This site uses Akismet to reduce spam. Learn how your comment data is processed.

You might also like: