Dark Mode

Retrieve data from your tables using Astra's GraphQL API.

After creating a table inyour keyspace using GraphQL and adding rows using GraphQL, you can retrieve data.

Prerequisites

Read data

Let's check that the data was inserted.

Now let's search for a particular record using a WHERE clause. The primarykey of the table can be used in the WHERE clause, but non-primary key columnscannot be used.The following query, looking at the locationhttp://localhost:8080/graphql/library[http://localhost:8080/graphql/library]will get both the title and the author for the specified book WHEREtitle:"Moby Dick" :

To find multiple books, an addition to the WHERE clause is required, to denote thatthe list of titles desired is IN a group:

To display the contents of a UDT, notice the inclusion of addresses in the values displayed for this read query:

To display the contents of a map collection, notice the inclusion of earned in the values displayed for this read query:

Filter options for reading

The filters available are:

  • eq (equal)
  • notEq (not equal) - limited use in conditional statements
  • gt (greater than)
  • gte (greater than or equal to)
  • lt (less than)
  • lte (less than or equal to)
  • in (within)
  • contains (a map contains the specified value)
  • containsKey (a map contains the specified key)
  • containsEntry (a map contains the specified key:value pair)

Note that these can only be used with primary key columns, just like in Cassandra, unlessindexing is created.

The next examples will query the same table, badge , using a variety of filters to illustratethe versatility of such filters.The first example finds the record that has the partition key badge_type equal to Gold , andthe badge_id equal to 100 :

Now if we use a different operator gt with the same query, notice that the query will fail,because no badge_id greater than a value of 100 is found:

In order to use filters for any columns that are not part of the primary key, currentlyyou need to use CQL to create a secondary index using the CQL shell.The next three examples show the CQL creation of an index in order to query a columnthat is a map collection.

In this example, an index is created on the keys of the map earned , so the containsKey filter can be used to query in GraphQL.

Because the index now exists, it is also possible to just filter based on the map key itself:

In this next example, an index is created on the values of the map earned , so the contains filter can be used to query in GraphQL.

To make a complete set of filters, an index is created on the entries of the map earned , so the containsEntry filter can be used to query in GraphQL.

Limiting and paging results using query options

You can add options to the query to limit and add paging to the results.

Add the limit option to set the maximum number of results returned by the query.

The query results can be paged by using the pageSize and pageState options.

Set pageSize to the number of results returned at each step.The default value of pageSize is 100.

The pageState value is returned in the data of the last query.Pass the value of pageState from the previous query to retrieve the next pagein the results.