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.
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.
Few characteristics of REST APIs:
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.
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.
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!
(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?
Fields
Fields is helpful to define to return data. Whatever field you select, you get the data for it.
Arguments
This feature is helpful to restrict the data for provided value. You can provide the arguments for as many fields as you want.
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.
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:
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.
Summary
GraphQL is query language for APIs, which uses the graphical lookups in behind the scenes. Below are few conclusion point.