Recently, RapidAPI officially launched our latest open-source project, RapidQL! With RapidQL, you can fetch, combine, and aggregate data from multiple APIs and databases all with a single request. This makes it much easier for you to build complex queries that utilize multiple data sources— all with a single query language! Today, we’re going to take a look at an example and how RapidQL simplifies your life as a developer.
For this tutorial, we’ll be building a simple app that gets information about a user from a MySQL database, pulls in the weather for that user’s location, and texts it to them using the phone number stored in the database.
Step 1: Fetching Users from MySQL DB
Since RapidQL works for both databases & APIs, you can pull data from both all in one query! The first step is going to be initializing a new instance of the RapidQL library. To do this, simply install the npm package by running:
$ npm install rapidql
Once the npm package has been installed, in your javascript file, you can require the module and initialize a new instance of rql:
const RapidQL = require('RapidQL'); const rql = new RapidQL({});
One great feature of RapidQL is the ability to provide default parameters or connection details in the RapidQL initialization. While RapidQL supports many different types of database connection like Postgres, MongoDB, and more, today we will be using MySQL. To add the connection details for MySQL, you will add the following to your initialization:
const RapidQL = require('RapidQL'); const rql = new RapidQL({ MySQL : { RQLDemo : { Host : process.env.MYSQLHOST, user : process.env.MYSQLUSER, password : process.env.MYSQLPASSWORD, database : process.env.MYSQLDATABASE } } });
Now that the database has been added, we can begin crafting our RapidQL request.
We’ll start by simply logging out the response of the request by using rql.log(); This function takes a RapidQL query as an input parameter, so we’ll add the following code to our index.js file:
rql.log(`{ MySQL.RQLDemo.rqlDemo.find(){ } }`)
When connecting to a database, the request follows the following schema:
DBType.DBName.Schema.Table.Operation
Since MySQL does not have a Schema associated with it, we can skip that part of the schema.
Let’s run the NodeJS file. You should receive the following output:
{ "MySQL.RQLDemo.rqlDemo.find": [ { "id": 1, "city": "San Francisco", "phoneNumber": "1777888999", "username": null, "email": null, "lat": "37.7749", "long": "122.4194", "customerId": 3 }, { "id": 2, "city": "Chicago", "phoneNumber": "1777888999", "username": null, "email": null, "lat": "41.8781", "long": "87.6298", "customerId": 2 }, { "id": 3, "city": "Tel Aviv", "phoneNumber": "1777888999", "username": null, "email": null, "lat": "32.0853", "long": "34.7818", "customerId": 1 }, ] }
One distinguishing factor of RapidQL, that is similar to GraphQL, is the ability to define exactly what information you want to receive in the response. Let’s say I only want the city & phone number of my users. I can achieve this by slightly altering my RapidQL query:
rql.log(`{ MySQL.RQLDemo.rqlDemo.find(){ city, phoneNumber } }`)
This simplifies our response to only show the following response:
{ "MySQL.RQLDemo.rqlDemo.find": [ { "city": "San Francisco", "phoneNumber": "1777888999" }, { "city": "Chicago", "phoneNumber": "1777888999" }, { "city": "Tel Aviv", "phoneNumber": "1777888999" } ] }
The other simplification of the response we can make is using the flattening response object feature of RapidQL. Adding a ‘-‘ before the object name in the query will remove that object and replace it with the contents of the object. In our example, I want to remove “MySQL.RQLDemo.rqlDemo.find”:
rql.log(`{ - MySQL.RQLDemo.rqlDemo.find(){ city, phoneNumber } }`)
Which then has the following response:
{ "0": { "city": "San Francisco", "phoneNumber": "1777888999" }, "1": { "city": "Chicago", "phoneNumber": "1777888999" }, "2": { "city": "Tel Aviv", "phoneNumber": "1777888999" } }
Step 2: Chaining Database Query into API Request
Now that we have our RapidQL query successfully calling our database, we can chain that request into an API query. To do this, we’ll want to set some default parameters that will be used to make a request on RapidAPI. Although we’re using RapidAPI, RapidQL works with any REST API you want!
In the same way that you can provide connection credentials during the initialization of RapidQL, you can also provide default headers that will always be sent with an API request. We’ll be using this to send our RapidAPI key with every HTTP request.
const RapidQL = require('RapidQL'); const rql = new RapidQL({ MySQL : { RQLDemo : { Host : process.env.MYSQLHOST, user : process.env.MYSQLUSER, password : process.env.MYSQLPASSWORD, database : process.env.MYSQLDATABASE } }, Http: { headers : { 'X-RapidAPI-Key' : process.env.RAPIDAPI_KEY } } });
Now that we have the default headers sending the RapidAPI Key, we can begin adding our API request to the RapidQL query. For this example, we’ll be using the Open Weather Map API. To add the request to Open Weather Map to our query, we can utilize the RapidQL code snippets that are available directly from RapidAPI. Our new request, with the API request added, will look like this:
rql.log(`{ - MySQL.RQLDemo.rqlDemo.find(){ city, phoneNumber, Http.get( url:"https://community-open-weather-map.p.rapidapi.com/weather", params: { "units": "imperial", "q" : city } ) { } } }`)
Again, this requests response with a lot of additional information that might not be required for our application, so let’s simplify it a bit:
We will do this by only requesting the type of weather (sunny, cloudy, rainy) and the current temperature. We’ll also flatten the response to simplify our data. The last simplification we will make is renaming one of our variables. The variable being used for the description of the current weather is called “main.” Since we have flattened this object, it is no longer a very descriptive variable name. We can change that by writing “currentWeather:main.” Here is our new request query:
rql.log(`{ - MySQL.RQLDemo.rqlDemo.find(){ city, phoneNumber, - Http.get( url:"https://community-open-weather-map.p.rapidapi.com/weather", params: { "units": "imperial", "q" : city } ){ - weather { main }, - main { temp } } } }`)
This will return the following response body:
{ "0": { "0": { "currentWeather": "Clouds" }, "city": "San Francisco", "phoneNumber": "1777888999", "temp": 51.44 }, "1": { "0": { "currentWeather": "Rain" }, "city": "Chicago", "phoneNumber": "1777888999", "temp": 33.37 }, "2": { "0": { "currentWeather": "Clouds" }, "city": "Tel Aviv", "phoneNumber": "1777888999", "temp": 50.54 } }
With the relevant information, we can get started implementing Twilio SMS to send our text message!
Step 3: Adding Another API!
This tutorial won’t go into the specifics of how to set up your account on Twilio, but you can follow this tutorial to subscribe, buy a phone number, and get ready to send an SMS message.
With your Twilio account setup, and a phone number purchased, we can use the Send SMS Message endpoint.
We will first use the code snippets to copy in a test request made in RapidQL. You want to be sure to fill out the require parameters such as from (the phone number you purchased when configuring Twilio), to, and body. For now, we will use “Test” as our SMS body.
Here is the updated RapidQL query:
rql.log(`{ - MySQL.RQLDemo.rqlDemo.find(){ city, phoneNumber, - Http.get( url:"https://community-open-weather-map.p.rapidapi.com/weather", params: { "units": "imperial", "q" : city } ){ - weather { currentWeather:main }, - main { temp }, Http.post( url:"https://twilio-sms.p.rapidapi.com/2010-04-01/Accounts/accountSid/Messages.json", params: { 'from':**From Phone Number**, 'to': **To Phone Number**, 'body': 'Test' } ) { } } } }`)
Be sure to run this code to ensure that you receive the correct “Test” SMS message.
With SMS messages being successfully sent, you can now customize the message based on the data pulled from the database and the Open Weather Map API. To do this, we will alter the “body” parameter to include the weather and temperature. Our body will be “The weather in {{location}} is {{temperature}} and {{weather}}”
Here’s our final query!
rql.log(`{ - MySQL.RQLDemo.rqlDemo.find(){ city, phoneNumber, - Http.get( url:"https://community-open-weather-map.p.rapidapi.com/weather", params: { "units": "imperial", "q" : city } ){ weather { currentWeather:main }, - main { temp }, Http.post( url:"https://twilio-sms.p.rapidapi.com/2010-04-01/Accounts/accountSid/Messages.json", params: { 'from':'14018889999', 'to': '13098889999', 'body': "The weather in {{city}} is {{temp}} and cloudy" } ) { } } } }`)
Final Thoughts
We hope this tutorial gave you a quick introduction to RapidQL, and an example of how you can use it to build a complex query that uses multiple data sources. As you can imagine, there are many possible ways to use RapidQL in your projects. Be sure to let us know what you end up building!
Leave a Reply