GraphQL is an open-source data query and manipulation language for APIs that allows clients to understand the data that your APIs expose and ask for only the portions they want. With GraphQL, you can create applications and services to aggregate data from multiple services or data stores. The Stargate open source project provides an API layer that allows you to expose a GraphQL API on top of any Cassandra database. Astra now offers offers a GraphQL API via Stargate, which we explore in this scenario.
We'll use the GraphQL API to:
- Create tables and records
- Read records from the tables
- Update records
- Delete records
As a prerequisite, be sure to setup an Astra account and create a Cassandra as a Service database at astra.datastax.com.
GraphQL is great stuff! Let's take a look.
This scenario is also available on our datastax.com/dev site, where you can find many more resources to help you succeed with Apache Cassandra™.
You're all set!
Thanks for taking a tour of Stargate's GraphQL API on Astra! For more details and elaboration of the GraphQL API features check out the Stargate documentation.
For more information and details on using the GraphQL API on Astra read the documentation.
Please give this scenario a rating (click on the stars near the top).
Have questions or more feedback? Send us a message.
Getting Started with Stargate GraphQL API for Apache Cassandra™
Setting up Environment Variables
Let's get coding!
Note: In this exercise, we will be using cURL commands in the terminal. The GraphQL section of the Astra User Interface explains how to do operations using GraphQL Playground, which is also a great tool. Because we are using cURL instead of GraphQL Playground in this scenario, we will sometimes refer you to the Document API section of the Astra UI to copy some commands, etc. for setting up the environment. These commands will work fine for the GraphQL API as well as the Document API. We point this out here to avoid any confusion.
New to our scenarios? Click here for three easy tips.
Tip 1 - You can click lines that begin with a triangle (like the following) to see further explanation.
Try clicking on this line.This is where you would find further explanation.
Tip 2 - To paste content into the terminal, right-click in the terminal on the right-hand side of the screen and select Paste.
Tip 3 - If a dark box has a bent arrow at the end, you can execute the command in that box by clicking on the box. The scenario will copy the command to the terminal and begin execution.
After you execute the command, the bent arrow changes to a check mark.
Bonus Tip! - You can click on images to enlarge them.
Thats it! You're good to go!
Step 1. Login to Astra.
Open a tab in the browser for Astra. You can login using a GitHub or Google account, or create a new Astra account.
Step 2. Go to the Connection tab of your Astra database.
From the Astra dashboard, click on the database name. Then, click the Connect tab.
Step 3. Copy the environment variables from the Astra Connect tab.
From the Connect tab, click Document API from the navigation on the left. Under Step 2 of the Prerequisites, click the copy icon to copy all the export commands.
Step 4. Paste the export commands into the terminal.
Back in the Katacoda terminal, paste the contents of the clipboard at the terminal prompt. All but the last of these export commands will execute. Do NOT execute the final command yet - we need to replace <app_token> before we can execute it.
Note: Do NOT execute the final export command yet!
Step 5. Find the Organization Settings.
From the Current Organization drop-down, select Organization Settings.
Step 6. Access Token Management.
In the left-navigation list, select Token Management.
Step 7. Generate a new application token.
From the Select Role drop-down menu, select Database Administrator. Click the Generate Token button.
Step 8. Copy the application token.
Click on the clipboard icon near the right edge of the token to copy the token to your clipboard. Please note that, as a security precaution, you will only be able to copy this token once. So, please take care not to overwrite the token until you have completed the next step.
Step 9. Replace <app_token> with the application token and execute the command.
Return to the Katacoda terminal. Delete <app_token> at the end of the final export command. Paste the copied token into the terminal at the end of the export command and hit enter to execute the command.
If the application token variable is set correctly, the following command displays the token value.