What Is GraphQL

Introduction

This would be a series of articles, in this article, we would talk about GraphQL, its comparison with REST and how it works, why it is used, and its operation types.

It will cover the following things,

  • What is GraphQL?
  • GraphQL vs REST
  • How does GraphQL works?
  • Why GraphQL?
  • Operation types - Query & Mutation

What is GraphQL?

  • GraphQL is a query language for your API that allows clients to request exactly the data they need, and nothing more. It was developed by Facebook and has gained widespread adoption as an alternative to REST APIs.
  • One of the main advantages of GraphQL is that it allows for more flexible and efficient data fetching. With a REST API, the server specifies the structure of the data that is returned to the client. This means that the client must make multiple requests to get all of the data that it needs, which can be inefficient.
  • With GraphQL, the client specifies the structure of the data that it needs, and the server returns exactly that data. This allows the client to retrieve all of the data that it needs in a single request, which can be more efficient and reduce the number of round trips between the client and server.
  • Another advantage of GraphQL is that it is self-documenting. The GraphQL schema defines the fields that are available for querying, and the types of those fields. This makes it easy for clients to discover and understand the data that is available in the API.
  • Overall, GraphQL can be a powerful tool for building APIs that are flexible, efficient, and easy to use.

How does GraphQL works?

  • It’s a normal Node (+ Express) Server!
  • ONE Single Endpoint (typically /GraphQL)
  • Uses POST because Request Body defines Data Structure of retrieved Data
  • Server-side Resolver Analyses Request Body, Fetches and Prepares and Returns Data

Why GraphQL?

  • Stateless, client-independent API
  • Higher flexibility than REST APIs offer due to custom query language that is exposed to the client
  • Queries (GET), Mutation (POST, PUT, PATCH, DELETE) and Subscriptions can be used to exchange and manage data
  • ALL GraphQL requests are directed to ONE endpoint (POST /GraphQL)
  • The server parses the incoming query expression (typically done by third-party packages) and calls the appropriate resolvers
  • GraphQL is NOT limited to React.js applications!

REST vs GraphQL

  • REST APIs are great for static data requirements (e.g. file upload, scenarios where you need the same data all the time)
  • GraphQL gives you higher flexibility by exposing a full query language to the client
  • Both REST and GraphQL APIs can be implemented with ANY framework and actually even with ANY server-side language

Operation types - Query & Mutation

Query

  • Retrieve Data (“GET”)
  • This is used to retrieve the data from the API
  • Here is an example of a query in GraphQL which requests a list of users and returns their ID, name, and email address.

query { users { id name email } }

Mutation

  • Manipulate Data (“POST”, “PUT”, “PATCH”, “DELETE”)
  • This is used to manipulate the data through the API
  • Here is an example of mutation in GraphQL. Create a new user with the name "John" and email "[email protected]". It returns the ID, name, and email of the newly created user.

mutation { createUser(name: "John", email: "[email protected]") { id name email } }

In this article, we talked about GraphQL, its comparison with REST and how it works, why it is used and its operation types, see you in next series of articles.

Happy learning!


Similar Articles