← Impulso Blog

Introduction to GraphQL

Agility and integrity are two of GraphQL’s principles. Learn more about the benefits of this language and the problems it solves!

GraphQL

Editorial translation by Impulso.

GraphQL is a query language specification for APIs based on the concept of graphs. A graph has several points called nodes, which are connected to one another. Each node can be thought of as a resource in the system. A resource might be a user, a customer, a person, a user’s country, a part supplied by a vendor, and so on. This lets you connect system resources in the form of a graph.

It also lets user interfaces interact with data declaratively and isolates them from how that data is resolved on the server. The client decides what information it needs, taking over a role that the server typically plays with REST.

GraphQL is also database-agnostic. This means you can use any database in your API.

What problems does GraphQL solve?

Created by the Facebook team in 2012, GraphQL was first used in the company’s apps. In 2015, the specification became open source, allowing several companies to adopt the technology in the years since.

Today, many companies use it, including Facebook, Amazon, GitHub, Twitter, PayPal, The New York Times, KLM, GetNinjas, Dailymotion, Shopify, and Pinterest. Even Netflix, which has its own project called Falcor, now uses it too.

According to Lee Byron, one of the developers of GraphQL, the problem Facebook faced, and the one that prompted it to create the solution, was getting the data needed for the news feed on mobile devices over slow network connections. Each post had to include a list of the users who liked it, and each user object had to include their name, photo, and profile link, along with information like whether they were already your friend.

Instead of creating an API for each different data structure and then piecing the data together, what if you could just tell “someone” what you need? That “someone” is GraphQL.

Benefits

Let’s look at some of the benefits of using GraphQL:

  • **Performance:
    **Requests are faster because the payload sent over the network is smaller. After all, only the necessary information is sent. With REST, we may need more than one request to get the information we need. GraphQL therefore improves application performance, with even bigger gains on slower or more limited networks, such as mobile networks.

  • **Versionless:
    **GraphQL takes a versionless approach, meaning the API doesn’t need versioning. You simply extend the schema to add new information. Older applications are unaffected because they won’t use that information.

    If you need to remove a field later, just mark it as deprecated, indicate which new field to use, and eventually remove the deprecated field from the schema.

Apollo’s principles

Apollo estimates that its framework is used in more than 90% of GraphQL implementations. The company outlines the following best-practice principles:

Integrity principles: Ensure that the GraphQL schema is well-defined, stable, and consistent.

  1. One GraphQL schema — The company should have one unified schema across all teams;
  2. Shared implementation — Teams should share the implementation;
  3. Track the schema in a registry — There should be a single source of truth for tracking the GraphQL schema.

Agility principles: Quickly deliver and adapt to the changes that are needed.

  1. Abstract, demand-driven schema — The schema should act as an abstraction layer that gives consumers flexibility by hiding implementation details;
  2. Agile approach to schema development — The schema should be built incrementally based on actual requirements and evolve over time;
  3. Improve performance iteratively — Performance management should be an ongoing process that adapts to changes in query loads and service implementations;
  4. Use metadata to empower developers — Developers should have visibility into the GraphQL API throughout the development process.

Operations principles: Deploy the GraphQL API securely to production.

  1. Access and demand control — Grant access on a per-client basis and manage what each client can access and how;
  2. Structured logging — Capture structured logs for all operations and use them to understand how the GraphQL API is used;
  3. Separate the GraphQL layer from the service layer — Adopt a layered architecture that puts GraphQL API functionality in a dedicated layer, separate from the individual services.

5 reasons to adopt GraphQL in your projects

  1. Strongly typed schema — The GraphQL schema is the “backbone” of the GraphQL API. It defines the operations (queries, mutations, and subscriptions) the API supports, including input arguments and responses. You could say the schema is the contract between the client and the API.
  2. No more overfetching or underfetching — Because the client specifies what data it needs, GraphQL solves the problems of receiving unnecessary data (overfetching) and having to make multiple requests because one call doesn’t return all the data (underfetching). This is considered one of GraphQL’s biggest benefits.
  3. Enables rapid product development — This is a big win, especially for frontend teams. With GraphQL, you can completely redesign a frontend’s UI without changing anything in the backend. With GraphQL Faker, frontend teams can also use API mocks.
  4. Composable GraphQL APIs — The schema lets you combine and merge multiple GraphQL APIs into one. Much like React components can be built from existing components, a GraphQL API can be composed of other APIs. This is especially helpful for clients, which would otherwise have to access multiple GraphQL API endpoints.
  5. An open-source ecosystem and an incredible community — Facebook officially launched GraphQL in 2012, and it became open source in 2015 (a pretty short time). Even so, it’s incredible how much the entire ecosystem has matured since then.

Want to learn more? Check out our meetup: 

https://youtu.be/V-dc3xe2CJE

References:

Related articles