[Note]GraphQL – Quick Guide (tutorialspoint.com) GraphQL is an open source server-side technology developed by Facebook to optimize RESTful API calls. It is an execution engine and a data query language. In this chapter, we’ll discuss the advantages of using GraphQL.

Why GraphQL

RESTful apis follow a clear and well-structured resource-oriented approach. However, as the data becomes more complex, the routes become longer. Sometimes data cannot be retrieved from a single request. This is where GraphQL comes in handy. GraphQL builds data in the form of graphs, and its powerful query syntax is used to traverse, retrieve, and modify data. Here are the advantages of using the GraphQL query language:

Ask what you want – and get it

Send a GraphQL query to your API and get exactly what you need. GraphQL queries always return predictable results. Application using GraphQL and stable. Unlike Restful services, these applications can limit the data that should be fetched from the server. The following example will help you understand this better: Let’s consider a business object Student with attributes ID, firstName, lastName, and CollegeName. Suppose a mobile application just needs to get firstName and ID. If we design a REST endpoint like/API /v1/ STUDENTS, it will eventually fetch data for all fields of a Student object. This means that data is being over-retrieved by RESTful services. This problem can be solved by using GraphQL. Consider the GraphQL query given below:

{
  {
    id
    firstName
  }
}
Copy the code

This will return only the values of the ID and firstName fields. This query does not get the values of the other properties of the student object. The response to the query described above looks like this:

{
    "data": {
        "students": [{"id": "S1001"."firstName": "Mohtashim"
            },
            {
                "id": "S1002"."firstName": "Kannan"}}}]Copy the code

Get multiple resources in a single request

GraphQL queries help to smoothly retrieve associated business objects, whereas typical REST apis need to be loaded from multiple urls. The GraphQL API gets all the data your application needs in a single request. Applications using GraphQL can be fast, even on slow mobile network connections. Let’s consider a business object, College, which has the following attributes: name and location. The Student business object has an association with the College object. If we use the REST API to get details about students and their universities, we end up making two requests to the server, such as/API /v1/ Students and/API /v1/colleges. This results in insufficient data retrieval per request. As a result, mobile applications are forced to make multiple calls to the server to get the data they need. However, mobile applications can use GraphQL to get the details of Student and College objects in a single request. Here’s the GraphQL query to get the data:

{
  students {
    id
    firstName
    lastName
    college {
      name
      location
    }
  }
}
Copy the code

The output of the above query contains exactly the fields we requested, as follows:

{
    "data": {
        "students": [{"id": "S1001"."firstName": "Mohtashim"."lastName": "Mohammad"."college": {
                    "name": "CUSAT"."location": "Kerala"}}, {"id": "S1002"."firstName": "Kannan"."lastName": "Sudhakaran"."college": {
                    "name": "AMU"."location": "Uttar Pradesh"}}, {"id": "S1003"."firstName": "Kiran"."lastName": "Panigrahi"."college": {
                    "name": "AMU"."location": "Uttar Pradesh"}}]}}Copy the code

Describe the possibilities of a type system

GraphQL is strongly typed and queries are based on fields and their associated data types. If there is a type mismatch in the GraphQL query, the server application returns a clear and useful error message. This helps client applications debug smoothly and detect errors easily. GraphQL also provides client libraries that help reduce explicit data conversion and parsing. Examples of Student and College data types are given below:

type Query {
  students: [Student]
}

type Student {
  id: ID!
  firstName: String
  lastName: String
  fullName: String
  college: College
}

type College {
  id: ID!
  name: String
  location: String
  rating: Float
  students: [Student]
}
Copy the code

Respond faster with powerful development tools

GraphQL provides rich development tools for both document and test queries. GraphiQL is an excellent tool for generating documentation of queries and their schemas. It also provides a query editor for testing the GraphQL API and intelligent code completion functionality when building queries.