How to get just the data you need with the Stargate GraphQL API in a simple coding scenario
Published: 2022-10-04 18:01:12


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39jafto39i3ls69j0lqq.jpeg)


_This is the second post in a series on how to use Stargate APIs. With our coding scenario and 15 minutes, you can learn how to use the Stargate GraphQL API right in your browser to solve some of the issues you might have experienced with REST API._

If you’ve been working with APIs for a while or went through [our first post in this series](https://medium.com/building-the-open-data-stack/qualify-your-database-needs-with-datastax-astra-stargate-rest-api-b62d76ccde7e) on how to get started with the [Stargate REST API](https://stargate.io/docs/stargate/1.0/developers-guide/rest-using.html) — you’ll be aware of the downsides to any REST API:



* **Over fetching:** The API will return more data than you need
* **Ambiguity:** There is no type of safety of the data you get with a REST API.
* **Wasted bandwidth:** The over fetching will cost bandwidth with multiple network requests

Like the [Stargate REST API](https://medium.com/building-the-open-data-stack/qualify-your-database-needs-with-datastax-astra-stargate-rest-api-b62d76ccde7e), the [Stargate GraphQL API](https://www.datastax.com/dev/graphql) is designed for Apache Cassandra® databases. However, it offers a significant advantage over REST APIs by allowing you to manipulate multiple data items with a single query and customize your queries to retrieve exactly the data you want. This ability eliminates over-fetching along with the extra work and wasted bandwidth resulting from it.

While you can add [Stargate GraphQL API](https://www.datastax.com/dev/graphql) to an existing Cassandra deployment, you don’t need a pre-existing database to start learning how to use it. If you don’t already have one, you can create a new Astra DB for free [here](https://astra.dev/3C4mDLW). Astra DB is a serverless database based on Cassandra and it comes with GraphQL API already enabled.

In just a few minutes, you can be up and running with our Katacoda scenario, which will show you how to use Stargate GraphQL API to run standard create, read, update, and delete (CRUD) operations on the data in your Cassandra database. While you don’t need to know [Cassandra Query Language (CQL)](https://cassandra.apache.org/doc/latest/cassandra/cql/) for this tutorial, having some familiarity with it will help you to better understand some of Cassandra’s idiosyncrasies, such as partition keys and clustering keys. If you’d like to dive deeper into Cassandra and CQL, DataStax has you covered with a free learning series on [Cassandra Fundamentals](https://www.datastax.com/learn/cassandra-fundamentals).

The technologies we’ll use in the scenario are:



* [Astra DB](https://astra.dev/3C4mDLW)
* [Stargate GraphQL API](https://www.datastax.com/dev/graphql)

You’ll also jump into the documentation for the Stargate Documents API but only briefly in order to copy the necessary export commands you will need when you’re setting up the environment for the scenario. You’ll just be copying those over into your terminal. These copied environment variables will be identical between the Documents API and GraphQL API so there is no harm from either tab.


# Exploring your options for using the Stargate GraphQL API

[GraphQL](https://graphql.org/) is an open-source query language and runtime used to fetch data from a supported API into your application. [Stargate GraphQL API](https://www.datastax.com/dev/graphql) was designed specifically for Cassandra and comes already enabled in Astra DB. Stargate GraphQL works by scanning your database and automatically creating HTTP endpoints to serve queries and mutations for the objects it finds in the database.

With the Stargate GraphQL API, you can get the data you need into your apps directly from Cassandra with a [CQL-first approach](https://docs.datastax.com/en/astra/docs/graphql-using-cql-first.html). A CQL-first approach directly translates CQL tables into GraphQL types, mutations, and queries and automatically generates the GraphQL schema from the keyspace, tables, and columns defined. You’ll get a standard set of mutations and queries for searching and modifying your table data but you won’t be able to do any customization of those.

If you want to use a more native GraphQL approach to access your data, you can use a [Schema-first](https://docs.datastax.com/en/astra/docs/graphql-using-schema-first.html) approach instead. This approach to using Stargate GraphQL reduces the need to know CQL because the creation and modification of the CQL tables in the Astra DB (table and column names for example) to match the GraphQL types are abstracted, and the resulting tables use GraphQL-centric names for types that correspond. Although you don’t have to give up all the control if you don’t want to since the GraphQL schema can be decorated with CQL annotations.

If you’ve used GraphQL before, you’ll find the schema-first approach familiar in how you create GraphQL types, mutations, and queries. One big advantage of this approach is that it allows you to deploy and update your schema without having to recreate the tables and columns directly.

Perhaps most importantly, a schema-first approach will allow you to customize your queries to eliminate over-fetching, which saves on bandwidth and improves the performance of your app.


# Using the Stargate GraphQL API with a schema-first approach

Our scenario uses cURL commands, so we’ll be using the terminal as opposed to the [GraphQL Playground](https://docs.datastax.com/en/astra/docs/develop/graphql.html#_using_the_graphql_playground). However, the GraphQL Playground is worth mentioning here because it offers another easy way to work with GraphQL. The server paths are structured to allow you to easily create new schemas and interact with the GraphQL APIs using built-in queries.

Getting back to our [scenario](https://katacoda.com/datastax/courses/astra-core-course/astra-graphql-api), once we get our environment ready to go in GraphQL, we’ll use a series of cURL commands to try out all the basic CRUD operations:



* Create a table and add some records to it
* Request a record from the table
* Update a record
* Delete a record

And to make the JSON output easier to read, we’ll include in our cURL commands some code to pipe the output into jq (a “pretty printer” for JSON).

With our [Katacoda scenario](https://katacoda.com/datastax/courses/astra-core-course/astra-graphql-api), you can learn the basics of using Stargate GraphQL API with Astra DB in your browser in just 15 minutes. Then you can start applying the same steps to work with your own Cassandra database or Astra DB. If you don’t have a database to work with yet, jump over to the [Astra DB](https://astra.dev/3C4mDLW) site to register for a free account and start learning today!

_Follow the [DataStax Tech Blog](https://datastax.medium.com/) for more developer stories. Check out our [YouTube](https://www.youtube.com/channel/UCqA6zOSMpQ55vvguq4Y0jAg) channel for tutorials and here for DataStax Developers on [Twitter](https://twitter.com/DataStaxDevs) for the latest news about our developer community._


# References:



1. [Katacoda scenario discussed in this article: Getting Started with Stargate GraphQL API for Apache Cassandra™](https://katacoda.com/datastax/courses/astra-core-course/astra-graphql-api)
2. [Other Astra Katacoda scenarios](https://katacoda.com/datastax/courses/astra-core-course)
3. [What is Astra DB? ](https://docs.datastax.com/en/astra/docs/)(Astra documentation)
4. [Astra Youtube tutorials](https://www.youtube.com/playlist?list=PL2g2h-wyI4SpWK1G3UaxXhzZc6aUFXbvL)
5. [Dig deep into Apache Cassandra™ katacoda scenarios](https://katacoda.com/datastax)
6. [Join our Discord: Fellowship of the (Cassandra) Rings](https://discord.com/invite/pPjPcZN)
7. [Stargate APIs | GraphQL, REST, Document](https://www.datastax.com/products/datastax-astra/apis)
8. [Cassandra Fundamentals](https://www.datastax.com/learn/cassandra-fundamentals)
9. [DataStax Academy](https://auth.cloud.datastax.com/auth/realms/CloudUsers/protocol/saml/clients/absorb)
10. [DataStax Certifications](https://www.datastax.com/dev/certifications)
11. [DataStax Workshops](https://www.datastax.com/workshops)
12. [Qualify Your Database Needs with DataStax Astra Stargate REST API](https://medium.com/building-the-open-data-stack/qualify-your-database-needs-with-datastax-astra-stargate-rest-api-b62d76ccde7e)
13. [Deploy a Netflix Clone with GraphQL and DataStax Astra DB](https://medium.com/building-the-open-data-stack/deploy-a-netflix-clone-with-graphql-and-datastax-astra-db-a784dd76295c)