GraphQL is a query language that describes how a client should request information through an application programming interface (API). In a broad sense, GraphQL is syntax  that  developers can use to ask for specific data and return that data from multiple sources. Once the client defines the structure of the data needed, the exact same structure of data is returned from the server.

Real-time updates to data can be made through GraphQL's open source reading, mutating and monitoring capabilities. GraphQL servers have been developed for use with popular coding languages such as JavaScript, Python, Ruby, C#, Go and PHP. The goal of GraphQL is to provide developers with a comprehensive view of data stored within an API, the ability to only receive relevant data and an architecture that makes APIs easier to scale and adapt over time. 

In order to build APIs using GraphQL, a GraphQL server that hosts the API and a client that connects to an endpoint are required. GraphQL APIs are composed of three characteristics: the schema, queries and resolvers:

  1. Schema- Is the type system used to define an API within a server implementation. All capabilities and functionalities are defined within the schema.
  2. Query- The request, or instruction, for output that is made. New queries are declared with a keyword and can support nested fields, arrays and arguments.
  3. Resolver- This tells GraphQL how and where data can be found in relation to the given fields. Without this, the GraphQL server would not know how to handle a query.

GraphQL helps to create flexible APIs that can be adjusted with changing needs and provides uniformity across applications. When applications need to be adjusted to add new or remove unused features, those corresponding fields in the query can simply be adjusted and executed with minimal effort and disruption.

Steps of a GraphQL query

Benefits of GraphQL

  • Organizes code in a hierarchical nature to reduce complexity and display relationships between objects.
  • Has strongly-typed fields that alert developers of error messages before running a query.
  • Returns predictable results from complex queries.
  • Outputs only relevant, desired data.
  • Allows applications to run faster and more stable.
  • Takes advantage of existing code and data sources, eliminating redundant work.
  • Is version free to support evolution and future changes.

GraphQL vs REST

Facebook developed GraphQL to simplify the endpoints of REST. Instead of having multiple endpoints with small amounts of disjointed data, GraphQL has a single endpoint that inputs complex queries and outputs only relevant information.

Using a metaphor, REST runs APIs as a sequential to-do list. It first runs errands, then picks up the mail and then makes a dentist appointment- all components are separate. With GraphQL, there is an added step before tasks are completed where the desired outcome is requested (in this case, the desired outcome is that the errands are completed, the mail is obtained and the appointment is scheduled). Then, GraphQL steps in and does all the tasks simultaneously and only returns the outcome, similar to a personal assistant.

REST has become the standard tool for developing web APIs but has shown to be too inflexible to keep up with rapid updates and susceptible to problems of over- and under- fetching . GraphQL provides a solution to these inefficiencies as well as offers a simpler, user-friendly interface.

This was last updated in February 2019

Next Steps

Hasura 2.0 expands GraphQL API-to-database connectivity

Continue Reading About GraphQL

Dig Deeper on Application development and design

Software Quality
Cloud Computing