Search code examples
cassandragraphqlnosqldatastax-astrastargate-oss

How can I query Cassandra with GraphQL using a non-primary key column?


I am using GraphQL for Cassandra database operation. The following search query work perfectly when filtering the column with partition key:

query oneUsers{
  users(value: { username:"username" }) {
    values {
      id
      name
      username
    }
  }
}

But getting following errors when trying to search other columns:

graphql: Exception while fetching data (/users) : org.apache.cassandra.stargate.exceptions.InvalidRequestException: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING

What is the best way to search columns in Cassandra using GraphQL?


Solution

  • You can only retrieve records from Cassandra by specifying (a) the partition key, or (b) the primary key column(s).

    If you would like to filter by non-primary key columns, you will need to create an index on the column. For example if you want to filter by id, create an index with:

    mutation createIndexes {
      users: createIndex(
        keyspaceName:"myks",
        tableName:"users",
        columnName:"id",
        indexName:"id_idx"
      )
    }
    

    You should then be able to query by id.

    For details and more examples, see Developing with the Astra DB GraphQL API. Cheers!