Connect to Hasura Programmatically
Up until now, you were taking advantage of YugabyteDB and Hasura’s Consoles to create, read, and update the data. In this final bonus section, you’ll learn how to connect to a Hasura public endpoint from your laptop and query records programmatically.
Query via Curl Tool
The curl command-line tool is one of the most straightforward and universal ways to check the connectivity between your laptop and the Hasura GraphQL endpoint.
Query the Todos table by sending a POST request to the Hasura endpoint:
- Locate the GraphQL API public IP address and Admin Secret in the settings of your Hasura project:
- Provide your API address and secret in the curl command below by replacing the GRAPHQL_API_ENDPOINT and GRAPHQL_ADMIN_SECRET placeholders:
curl -X POST \-H "Content-Type: application/json" \-H "X-Hasura-Admin-Secret: GRAPHQL_ADMIN_SECRET" \-d '{"query":"query {todos {title user {id name}}}"}' \GRAPHQL_API_ENDPOINT
- Execute the curl request and confirm the output is as follows:
{"data": {"todos": [{"title": "Complete Hasura & YugabyteDB Tutorial","user": {"id": 1,"name": "Mark"}}]}}
Query via Node.js Application
Callout: To proceed with this section you need to download and install npm and Node.js.
Finally, download and execute a sample Node.js application that connects to your Hasura BaaS and retrieves a list of Todos:
- Clone the project with the sample application:
git clone https://github.com/yugabyte/examples.git && cd examples/yugabyte-hasura-snippets/
- Add the minimal GraphQL client for Node to the project:
npm add graphql-request graphql
Open the
examples/yugabyte-hasura-snippets/app.js
file.Provide your GrahpQL API address and Admin Secret in the
app.js
source code by replacing the GRAPHQL_API_ENDPOINT and GRAPHQL_ADMIN_SECRET placeholders.Launch the application:
node app.js
- Confirm it produces output as follows:
{"todos": [{"title": "Complete Hasura & YugabyteDB Tutorial","user": {"id": 1,"name": "Mark"}}]}
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs