Polski

Français

Deutsch

日本

Español

Nederlands

Dansk

Log in to GraphQL Editor
RESOURCES

What is GraphQL used for?

GraphQL is a way of communicating with APIs created as an alternative to REST.

01A new query language

GraphQL is a query language for APIs that was developed by Facebook and their dev community. It premiered in June 2018, and is now a mature technology that has gained widespread market recognition. As a result, it is well recognized and many companies are choosing to implement it. In order to tell you how it is different, we have to look at what is still most widely used: REST and what it is based on. GraphQL is often called the successor to REST because it solves its key problems. As you probably know, it is based on a kind of old system design. According to it, each data model is available at a separate address. Having an online shop with clothes, for example we can have addresses for cart, warehouse, products and many more.

GraphQL Query Language

02Get only what you wanted

In fact, the number of such addresses will never be small, on the contrary, it will grow with the system. Keep in mind that for almost every address you need to create a set of calls to handle get, post, put or delete requests. This will result in a large number of addresses to maintain and an even larger amount of calls needed to get the data. GraphQL works in a completely different way. We have only one address, a single source of truth, from which we can request data. What we get depends solely on us and how we construct the query.

GraphQL Queries

03Flexible queries

The key feature of GraphQL is the ability to perform flexible queries within which we define exactly what data from the API we want to get. In addition, the language provides the possibility of creating nested queries, which makes it gain even more of an advantage over REST APIs in these areas.

GraphQL Queries

04Platform agnostic

One point of similarity between REST and GraphQL is platform or programming language independence. Another common feature is the idempotency of the same operations. For both REST architecture and GraphQL, operations such as retrieve, modify or delete can be used repeatedly without changing the result.

REST vs GraphQL

05Basic elements

The basic elements of GraphQL are: mutation:

  • query: a basic fetch operation in GraphQL which lets us get data. the key difference is we get exactly the data we requested by structuring the query to get only what we want
  • mutation: a basic data manipulation operation in GraphQL which lets us create, modify or delete data
  • schema: the foundation of every GraphQL project, it describes the logic and functionalities, is written in SDL (Schema Definition Language) and it’s main components are types and fields resolver: the function used for connecting your GraphQL schema to the backend, it turns operations into data and can return strings, integers, null and other primitives
  • resolver: resolver: the function used for connecting your GraphQL schema to the backend, it turns operations into data and can return strings, integers, null and other primitives
GraphQL Mutations

06Schema Definition Language

GraphQL uses its own syntax: SDL or Schema Definition Language. It is used to specify the schema of an API and its types and fields. Types are declared using the type keyword and fields are added in brackets for each object type.

GraphQL Schemas

07Growing community

While GraphQL is still not as popular as REST, its community is rapidly growing. It has already had a number of major adopters such as GitHub, Netflix, PayPal or Shopify. It’s only a matter of time before more will follow.

GraphQL Community

GraphQL

  • No over or under fetching: GraphQL uses flexible or even nested queries which ensure you get exactly and only what you need. No multiple calls needed and no excess data received, everything can be handled by one specifically constructed query.
  • Single endpointSimilarly you only need to have a single endpoint. The query structure will handle getting what you need, how you have the data organized is irrelevant, as long as you correctly construct the query it will get everything you need even from a complete mess.
  • Type-safetyGraphQL schemas use a strong type system, this means all APIs exposed to the client are written in the Schema Definition Language which eliminates data inconsistencies and makes finding errors much easier.

REST

  • Hierarchy: REST APIs use Uniform Resource Identifiers or URIs to address resources. This means everything follows a simple hierarchical approach, which is fine for small projects but can cause a lot of issues for larger or more complex projects.
  • Multiple round-trips: Because of the hierarchical structure a REST API usually has a number of endpoints and in turn the client needs multiple calls to get all the needed data.
  • Over and Under-fetching: The hierarchical structure and lack of flexible queries means REST APIs will usually fetch either too much unnecessary data or will need multiple calls to get all the needed data. This obviously impacts both bandwidth and performance and is usually compounded the larger the application is.

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor