A closer look at GraphQL

Ashish Sahay Mar 10, 2022 5:29:15 PM

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools. (Source: graphql.org)

In today’s era of application development we need data fetch and update mechanisms which can be easy to maintain and easy to use. If your app needs to show data in real time from the server and enhance the user experience for your app, APIs are the best fit. APIs give real time data to the end application and data can also be pushed back to server with inputs. All the business logic stays at the server side.


Find out how we can help you build solutions using Oracle APEX with our award winning application development and consultancy services.

Book a Meeting




Now let’s consider REST and GraphQL for comparison and of course for analysis. REST APIs are great fit for fetching the data in bulk and of course we can design the custom output. However, if we need to fetch limited number of columns / datasets from an existing API, this option is not available in REST as of now. The typical solution to this is to make changes to the API itself.

GET Response

Few characteristics of REST APIs:

  1. Response data is always designed and controlled by server.
  2. If you need multiple employees’ data from one API you have to call it multiple times
  3. Since the server controls it, you cannot have your own version, it’s derived from API. (Static Version).

GraphQL however, is query language for creating APIs. You can have your own version of data. GraphQL doesn’t control any dataset when it comes to column returned. You can query data in your desired shape and size.

GraphQL

GET Response Query


Ask for what you need, get exactly that

Using GraphQL you are free to use whatever dataset you want. In this above example we are simply fetching ‘ename’ and ‘job’ from available dataset for ‘empno 7788’. With REST API to achieve the same result we would have to make code changes to the API, which is tedious and costly. GraphQL however, gives you exactly whatever is queried, nothing less, nothing more.

Hero, name, mass, height

GraphQL queries are capable of fetching data from multiple sources using single request. However, in REST we need to load multiple URLs. For example, if we need data from an EMP table and DEPT table, we can simply select from a GraphQL query in one go. And of course, if you only want data from one table then just select required fields from one table. Simple!

picture_03(Source: graphql.org)

GraphQL was built and published by Facebook and is proving popular. Some users include Facebook itself, while others are companies such as Github, Pinterest and Shopify.

GraphQL and APEX

Let’s explore how we can leverage it in Oracle APEX?

We have apex_web_service.make_rest_request utility to call any API. We can use the Any GraphQL API by using this utility package and furthermore use the returned data for application.
How to Query and Update the data in GraphQL?

 

  • Queries: Queries are used to fetch the desired data
    • Fields
    • Aliases
    • Arguments
    • Fragments
  • Mutations: To update the data on server

Fields

Fields is helpful to define to return data. Whatever field you select, you get the data for it.

R2D2


Arguments


This feature is helpful to restrict the data for provided value. You can provide the arguments for as many fields as you want.

Luke Skywalker 5.6

In the above example we are fetching data for id = 1000 and height unit = FOOT.

Aliases

In GraphQL, you cannot select one field multiple times, but if you use aliases for that, it becomes possible.

Empire hero

In the above example we are fetching hero twice using aliases.

Fragments

Fragments are reusable component in GraphQL. You can define the Data format in fragment and call in multiple places. Using Fragments you are essentially creating a template and using it repeatedly to fetch the data. Lets’ take an example of fetching data for two books using fragments:

Fragment


Mutations


As we have seen, GraphQL queries are easy and optimised to fetch the data. GraphQL provides the Mutations to modify the data on server.

mutation


Summary

GraphQL is query language for APIs, which uses the graphical lookups in behind the scenes. Below are few conclusion point.

  • GraphQL is totally managed within the API however the REST is Server driven.
  • GraphQL allows to select the desired data but REST returns predefined data.
  • We can fetch data multiple time by one single API call in GraphQL but in REST APIs we have to call the API multiple times or modify the REST API.
You have the benefit of selecting data from multiple datasets from single API and Single hit on the server. Since we can call these APIs from APEX as well, we can utilise them within our applications when fetching data.

We can fetch the data on page load using these GrapgQL APIs. And if we need to add additional data to the APEX page, we don’t have to modify the GraphQL API. We can simply change the GraphQL query to fetch additional data.

Since GraphQL provides the mutations, we can also modify data from APEX to the GraphQL server.

So in conculsion, GraphQL can be used to reduce the efforts and work with APIs effectively.

If you are interested in our Oracle APEX Services you can speak to one of our development specialists today, visit Oracle APEX Consultancy or email us at enquiries@dsp.co.uk.