Accessing Cassandra with GraphQL

APIs are the key to cloud-native architectures that are scalable and decoupled, and GraphQL is a promising API strategy!

Get Started
Introduction

Introduction

APIs are the key to cloud-native architectures that are scalable and decoupled, and GraphQL is a promising API strategy! With GraphQL, you can create applications and services to aggregate data from multiple services or data stores. Originally developed by Facebook in 2012, but publicly released in 2015, GraphQL is an open-source data query and manipulation API protocol.

Next concept: Why GraphQL for Cassandra?

Why GraphQL for Cassandra?

While REST is a more ubiquitous API protocol, GraphQL improves upon some of the limitations of REST. GraphQL uses a type-safe, graph-based schema. This means clients can query and manipulate specific data items on a single access. Nodes in the graph-based schema represent objects and edges represent relationships. Queries amount to using a single URL endpoint to perform graph traversals, which access dictionaries of values in each node. This means clients can use sophisticated queries to get tailored data in a single access, instead of multiple round trips using heavy payloads of entire records.

These seemingly simple improvements become significant performance benefits when used in cloud-native architectures at scale. And, if you want performance benefits at scale, use a GraphQL API for your Cassandra Storage!

Next concept: How to Get GraphQL With Cassandra
Why GraphQL for Cassandra?
How to Get GraphQL With Cassandra

How to Get GraphQL With Cassandra

You have several choices on how to get GraphQL with Cassandra. You can always build a GraphQL API for your Cassandra cluster. Or, if you don’t have a Cassandra cluster and want one, you can create a database using Astra, which has a built-in GraphQL API. You can try out Astra Online (DataStax Cassandra as a Service) and set up a database for free at astra.datastax.com.

Here’s an example of GraphQL access using JavaScript:

Bash
// GraphQL
// @param Object primary_key
// @param Array, return_fields
async gql_read( primary_key, return_fields = [] ){
  if (return_fields == []) throw new Error('GraphQL API requires specifying return fields')
  let prop_string = make_special_json( camelcase_props( primary_key ))
  let return_fields_string = return_fields.map(camelcase).join(' ')
  let gqlModel = camelcase( this.tablename )
  // Example of the GraphQL query which will be generated:
  // Given this.tablename: "user_blessings"
  //   and primary_key: {userid:'kiyuzg', year:2020, when:'2020-07-09 03:48:32'}
  //   and return_fields: ['userid', 'year', 'when','where_location','blessing']
  //
  //   query {
  //      userBlessings ( value: {userid:"kiyuzg", year:2020,
  //      when:"2020-07-09 03:48:32"}){
  //
  //  In addition to the comments under gql_create(), note that return values
  //  are separated by spaces, which feels weird for me in JavaScript.
  let result = await post()( gqlURL,
                              {query:
                                `query {
                                   ${gqlModel} ( value: ${prop_string} ){
                                   values { ${return_fields_string} }}}`
                              })
  if( result && result.data && result.data[gqlModel] &&
  result.data[gqlModel].values && result.data[gqlModel].values.length ){
    return result.data[gqlModel].values
  } else {
    return []
  }
}

Skill Building

Try The Astra GraphQL API Service for yourself - it allows you to scale your databases horizontally quickly, and easily. Give it a try!

Continue Your Journey

How to Build APIs for a Cassandra Database

How to Build APIs for a Cassandra Database

Cedrick Lunven shows how to create APIs for your distributed database including REST, gRPC and GraphQL.

Watch Now
Using the Astra GraphQL API
GraphQL

Using the Astra GraphQL API

Astra has a GraphQL API that allows you to create tables and query your table data

Start Learning
GraphQL and Astra
GraphQL

GraphQL and Astra

Review this example of adding and removing data using Astra and GraphQL

Explore

More Resources

Items related to GraphQL

Designing APIs

Designing APIs

Watch a demonstration of how to create REST, gRPC and GraphQL APIs for Cassandra.

Watch Now
GitHub Example: GraphQL
GraphQL

GitHub Example: GraphQL

Learn, by example, several technical reasons why GraphQL is a good fit for an Apache Cassandra™ application.

Download Now
Website: GraphQL.org
GraphQL

Website: GraphQL.org

Here's the main site for GraphQL resources, with articles, documentation and examples.

Read more

GraphQL Playground: Visit