Graphql Engine Hasura Flow
Introduction
This flow is a part of the Graphql Engine Examples Collection. The Hasura collection flow demonstrates how to use the GraphQL engine in combination with Node-RED.
To read more about GraphQL engine Hasura, please refer to this documentation: …
Important: for more clarity, comments have been added throughout this example flow. It is recommended to read these comments while reviewing the documentation to gain a better understanding of each component’s role and functionality.
Prerequisites
This flow along with other dependencies and flows is available to user in the following package:
@Senlab/node-red-hasura
To learn more about Verdaccio and how to install packages, please refer to this documentation: Verdaccio
For the example to work, both the GraphQL engine and a database service need to be running on Sandbox. In this case, we’ve chosen the PostgreSQL database. For instructions on how to install services on Sandbox, please refer to the following documentation: services.
IMPORTANT: GraphQL engine is used for applying queries to the supported database using HTTP Requests. For this to work Table and database need to be tracked by Hasura, to learn how to configure that, please refer to this documentation: …
Line 1: Creating Test Database
[image of the line 1]
The first line of the flow, marked with the comment ``Line 1,'' is dedicated to generating and initializing a PostgreSQL table with the data needed to test our GraphQL engine. This line is identical to one found in our SQL Helper Flows Collection. For more details on the implementation and process of this flow segment, please refer to this documentation: …
Line 2: Testing Table with Select Query
[image of line 2]
The second line checks if the table was successfully created by generating and executing a Select query. This step ensures that the table is properly set up for further testing. Like the first line, this part of the flow is also used in another collection PostgreSQL Example Flows. For more information, please refer to this documentation: …
Initiating flow with Inject Node
We initiate our flow with the Inject Node, which is a basic node and already preinstalled inside Node-RED. It can be found under the common tab.
The Inject Node can function as a button or a signal generator. Users need to specify which property and value the node will inject into the flow. For the purposes of this flow, the specific configuration of the property and value is not critical, as the primary goal is to activate other nodes within the flow. In the configuration panel, users have the option to set the Inject Node to either inject the value just once or to do so repeatedly at a specified interval.
Using Function Node to generate HTTP Request
Function Node is powerful Node-RED node which enables us to add our Custom Javascript code to Node-RED without the need to make custom nodes.
Instead of hardcoding HTTP Request body or URL inside HTTP Request node, function node can be used, where URL and body of the request can be dynamically constructed from incomming data, this example still has hardcoded headers and request body, but it shows how those parameters can be passed from the function node inside HTTP Request node, this way is also more readable.
Headers must be passed inside the msg.headers property and body of the message must be JS object.
msg.headers = {
'Content-Type': 'application/json'
};
msg.payload = {
query: `
query {
hasura_test {
variable_test
variabletesting
}
}
`
};
return msg;
This example shows how to construct msg.payload or HTTP POST Request body to initiate Select Query from the PostgreSQL database through Hasura. This specific query is requesting variables variable_test and variabletesting from hasura_test table.
HTTP Request Node
The HTTP Request Node is a fundamental component within Node-RED, utilized for executing various HTTP requests, a prevalent communication method among IoT devices.
When parameters like URL and message body are supplied by a preceding function, the configuration required involves selecting the type of request and the type of return object. In this scenario, since POST requests are being made and the response is irrelevant to subsequent flow activities, the method is set to POST, and the response handling can be disregarded.