GraphQL

Get many resources in a single request

Sandaminimadhushika
4 min readJun 4, 2021

GraphQL is a query language for APIs and provides a clear and complete description of the data in your API. GraphQL APIs get all the data your app needs in a single request, while typical REST APIs require loading data from multiple URLs.

In other words, you can get all the data for your app using only single request.

After hearing about it, I was so curious to try it out. In this article, I will share some idea about GraphQL query language and its features.

Intro to GraphQL

GraphQL was developed by Facebook in 2012. They developed it because their team needed “a powerful and flexible data-fetching API to describe all of Facebook”. Also, this is simple and easy to learn when building the mobile applications. It’s a way to get data from an API into your application. And as you will see, it does this in a much more simple and efficient manner than traditional methods and services. GraphQL provides a flexible and rich technology for extracting data that is more efficient than REST APIs to mobile and web developers. GraphgQL allows the client to mention exactly what data is desired and allows to get multiple queries in a single request.

GraphQL vs REST

In typical REST API has a specific endpoint and once we hit on that, entire block of data comes back as a JSON response. But the GraphQl consists of Schemas, Queries, and Mutations which allows you to get only specific piece of data, not just the entire block. However, it is important to highlight that GraphQL and REST are two different things. GraphQL is a language and a technology. But REST is an architecture pattern. Although developers more attract to the GraphQL, REST will not destroy due to this difference.

GraphQL Schemas

GraphQL schema consists of type definitions. Each type consists of one or more fields. Those fields get one or more arguments and return a specific type.

The graph is organized in such a way that these fields are nested together. Note that the graph does not have to be non-circular and circular graph is fully acceptable. That is, a client can access its child from a single field, but cannot automatically return to its parent unless explicitly defined in the schema.

An example GraphQL Schema for a library may contain the following definitions, describing a Book, an Author of the book and a root query to get the books of the Author in the library.

Screenshot by Author

“!” at the end of some names indicates that this is a nullable type. Types that do not have this item can be empty as a response from the server. The GraphQL service handles them correctly so that you can request nullable subfields safely.

GraphQL Query

A GraphQL has two operations. Those are read or a write operation. A GraphQL query is used to read or fetch values. In either case, the operation is a simple string that a GraphQL server can parse and respond to with data in a specific format. The most popular GraphQL response format that is usually used is JSON.

Screenshot by Author

Also, you can create nested queries in GraphQL. You can add new fields inside the existing field and get more data from it. Following example shows you, how you can use nested queries to extract data.

Screenshot by Author

GraphQL Mutation

GraphQL Mutation is used to modify the server side data. Mutation is similar to the state changing methods in REST (like DELETE,PUT,PATCH). To call a mutation, you must use the keyword ‘mutation’ before the GraphQL query. To pass the input data, you have to provide the data written as a JSON object.

Screenshot by Author

Conclusion

GraphQL is a query language for APIs and you can extract data from a single request. This means smaller queries and lesser traffic over the network; which in turn reduces the response time. But you’re not supposed to entirely rely on it. We have discussed some of the awesome features of GraphQL like GraphQL Schemas, queries and mutations. Also we talked about the difference between GraphQL and REST API. I hope they will be useful in your apps.

That’s all for this article. Thank you for reading!!

--

--