Advantages of GraphQL

Here are several advantages of GraphQL protocol relevant to API clients integrating with a GraphQL endpoint.

1. Speed

GraphQL is faster than other communication APIs because it facilitates you to cut down your request query by choosing only the specific fields you want to query.

2. No over-fetching and under-fetching problems

The main advantage of GraphQl over REST is that REST responses contain too much data or sometimes not enough data, which creates the need for another request. GraphQL solves this problem by fetching only the exact and specific data in a single request.

3. Hierarchical Structure

GraphQL follows a hierarchical structure where relationships between objects are defined in a graphical structure. Here, every object type represents a component, and every related field from an object type to another object type represents a component wrapping another component.

4. Defines a data shape

When we request GraphQL queries to the server, the server returns the response in a simple, secure, and predictable shape. So, it facilitates you to write a specific query according to your requirement. This makes GraphQL really easy to learn and use.

Examples

Let’s say, you want to get room stays with arrival, departure dates, and a list of arriving guest names

GraphQL

With GraphQL there would be just one request and it would look like this:

query ArrivingPrimaryGuests($date: Date!) {
  room_stays(
    filter: { reservation_from: { gt: $date } }
  ) {
    edges {
      node {
        id
        arrival: reservation_from
        departure: reservation_to
        first_guest {
          firstname
          lastname
        }
      }
    }
  }
}

In response you would get a structured resultset with the information you requested only:

{
  "data": {
    "room_stays": {
      "edges": [
        {
          "node": {
            "id": 1,
            "arrival": "2022-02-21",
            "departure": "2022-02-28",
            "first_guest": {
              "firstname": "Matt",
              "lastname": "Luis"
            }
          }
        },
        {
          "node": {
            "id": 2,
            "arrival": "2022-02-22",
            "departure": "2022-02-26",
            "guests": {
              "firstname": "Linda",
              "lastname": "Webber"
            }
          }
        }
      ]
    }
  }
}

REST

Info

REST protocol doesn’t define how certain things (like filtering or selective structure of the resultset) should or can be implemented. The purpose of the following example is to show the difference between typical GraphQL and REST implementations.

When it comes to working with multi-dimensional data structures, the REST implementation of the API can differ but, normally, you would have at least 2 steps to take and different types of requests to make:

Step 1

You would request room_stays with one request:

https://3rpms.de/api/room_stays?filter[]=...

In response, you would get a resultset with all information about the room_stay. Even information you don’t need.

[
  {
    "id": 1,
    "arrival": "2022-02-21",
    "departure": "2022-02-28",
    "lodgingsGross": "693.00",
    "additionalSales": "0.00",
    "reservation_from": "2022-02-24",
    "reservation_to": "2022-03-03",
    "check_in": null,
    "check_out": null,
    "selfcheckout_enabled": false,
    "selfcheckout_url": null,
    "mealNotes": "",
    "maidNotes": "",
    "notes": "",
    "createdAt": "2021-01-12T10:44:43+01:00",
    "updatedAt": "2021-01-12T10:44:43+01:00"
  },
  {
    "id": 2,
    "arrival": "2022-02-22",
    "departure": "2022-02-26",
    "lodgingsGross": "693.00",
    "additionalSales": "0.00",
    "reservation_from": "2022-02-22",
    "reservation_to": "2022-02-26",
    "check_in": null,
    "check_out": null,
    "selfcheckout_enabled": false,
    "selfcheckout_url": null,
    "mealNotes": "",
    "maidNotes": "",
    "notes": "",
    "createdAt": "2021-01-12T10:44:43+01:00",
    "updatedAt": "2021-01-12T10:44:43+01:00"
  }
]

Step 2

Please, note that the request we performed in Step 1 returned room_stay records only but didn’t provide information about guests, as guests are a subset of multiple records for each room_stay. Usually, REST implementations foresee that subsets can be fetched exclusively when required in order not to do extra computing work when it might not even be necessary.

You would now have to request information about guests with the following request for each room_stay record:

https://3rpms.de/api/room_stays/<room_stay.id>/guests

In response, you would get a resultset with all information about the guests but again - packed with information, you don’t even need,

[
  {
    "title": "Mr",
    "firstname": "Matt",
    "lastname": "Luis",
    "language": "de",
    "email": "matt.luis@email.io",
    "telephone": "+49 12345677",
    "mobile": "+49 23456789"
  },
  {
    "title": "Mrs",
    "firstname": "Martha",
    "lastname": "Luis",
    "language": "de",
    "email": "matt.luis@email.io",
    "telephone": "+49 12345678",
    "mobile": "+49 23456790"
  }
]