Introduction to the GraphQL

What is GraphQL?

GraphQL is a query language and runtime system for APIs (Application Programming Interfaces) that was created by Facebook in 2012 and released as an open-source project in 2015. GraphQL allows clients to request only the data they need and nothing more, and allows servers to respond with exactly the requested data, rather than a pre-defined set of data. This makes it more efficient and flexible than traditional REST APIs.

In a GraphQL API, the client specifies the data it needs using a GraphQL query, which is a string that looks similar to JSON. The query is sent to the server, which responds with a JSON object that matches the structure of the query. GraphQL also supports mutations, which allow clients to modify data on the server, and subscriptions, which allow clients to receive real-time updates when data changes.

GraphQL has become increasingly popular for building modern web and mobile applications, and is supported by a growing number of programming languages, frameworks, and tools.

Why We use GraphQL?

There are several reasons why developers and organizations use GraphQL:

  1. Efficient data fetching: With GraphQL, clients can specify exactly the data they need, reducing the amount of data that needs to be transferred over the network. This can improve performance and reduce the load on the server.
  2. Flexibility: Because GraphQL allows clients to specify the exact data they need, it can be easier to evolve the API over time. Changes to the data model can be made without breaking existing clients, as long as the changes are communicated properly.
  3. Self-documenting API: GraphQL APIs are self-documenting, which means that clients can easily discover the available data and operations without requiring separate documentation. This can save development time and reduce errors.
  4. Strongly-typed schema: GraphQL APIs have a strongly-typed schema, which means that the API contract is clearly defined and enforced. This can help catch errors early in the development process.
  5. Tooling: The GraphQL ecosystem includes a growing number of tools and libraries that can help with development and testing, including code generators, IDE integrations, and testing frameworks.

Overall, GraphQL can be a powerful tool for building efficient and flexible APIs that can be easily consumed by modern web and mobile applications.

Get many resources in a single request

GraphQL queries help to smoothly retrieve associated business objects, while typical REST APIs require loading from multiple URLs. GraphQL APIs fetch all the data your application need in a single request. Applications using GraphQL can be quick even on slow mobile network connections.

Let us consider one more business object, College which has the attributes: name and location. The Student business object has an association relationship with the College object. If we were to use a REST API in order to fetch the details of students and their college, we will end up making two requests to the server like /api/v1/students and /api/v1/colleges. This will lead to under fetching of data with each request. So mobile applications are forced to make multiple calls to the server to get the desired data.

However, the mobile application can fetch details for both Student and College objects in a single request by using GraphQL.

The following is a GraphQL query to fetch data −

{
   students{
      id
      firstName
      lastName
      college{
         name
         location
      }
   }
}

The output of the above query contains exactly those fields we have requested for as shown below −

{
   "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"
            }
         }
      ]
   }
}

Describe what’s possible with a type system

GraphQL is strongly typed and the queries are based on fields and their associated data types. If there is type mismatch in a GraphQL query, server applications return clear and helpful error messages. This helps in smooth debugging and easy detection of bugs by client applications. GraphQL also provides client side libraries that can help in reducing explicit data conversion and parsing.

An example of the Student and College data types is 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]
}

Move faster with powerful developer tools

GraphQL provides rich developer tools for documentation and testing queries. GraphiQL is an excellent tool which generates documentation of the query and its schema. It also gives a query editor to test GraphQL APIs and intelligent code completion capability while building queries.

Introduction to the GraphQL
Show Buttons
Hide Buttons