• Glossary
  • Support
  • Downloads
  • DataStax Home
Get Live Help
Expand All
Collapse All

DataStax Astra DB Classic Documentation

    • Overview
      • Release notes
      • Astra DB FAQs
      • Astra DB glossary
      • Get support
    • Getting Started
      • Grant a user access
      • Load and retrieve data
        • Use DSBulk to load data
        • Use Data Loader in Astra Portal
      • Connect a driver
      • Build sample apps
      • Use integrations
        • Connect with DataGrip
        • Connect with DBSchema
        • Connect with JanusGraph
        • Connect with Strapi
    • Planning
      • Plan options
      • Database regions
    • Securing
      • Security highlights
      • Security guidelines
      • Default user permissions
      • Change your password
      • Reset your password
      • Authentication and Authorization
      • Astra DB Plugin for HashiCorp Vault
    • Connecting
      • Connecting to a VPC
      • Connecting Change Data Capture (CDC)
      • Connecting CQL console
      • Connect the Spark Cassandra Connector to Astra
      • Drivers for Astra DB
        • Connecting C++ driver
        • Connecting C# driver
        • Connecting Java driver
        • Connecting Node.js driver
        • Connecting Python driver
        • Drivers retry policies
      • Connecting Legacy drivers
      • Get Secure Connect Bundle
    • Migrating
      • FAQs
      • Preliminary steps
        • Feasibility checks
        • Deployment and infrastructure considerations
        • Create target environment for migration
        • Understand rollback options
      • Phase 1: Deploy ZDM Proxy and connect client applications
        • Set up the ZDM Automation with ZDM Utility
        • Deploy the ZDM Proxy and monitoring
          • Configure Transport Layer Security
        • Connect client applications to ZDM Proxy
        • Manage your ZDM Proxy instances
      • Phase 2: Migrate and validate data
      • Phase 3: Enable asynchronous dual reads
      • Phase 4: Change read routing to Target
      • Phase 5: Connect client applications directly to Target
      • Troubleshooting
        • Troubleshooting tips
        • Troubleshooting scenarios
      • Additional resources
        • Glossary
        • Contribution guidelines
        • Release Notes
    • Managing
      • Managing your organization
        • User permissions
        • Pricing and billing
        • Audit Logs
        • Configuring SSO
          • Configure SSO for Microsoft Azure AD
          • Configure SSO for Okta
          • Configure SSO for OneLogin
      • Managing your database
        • Create your database
        • View your databases
        • Database statuses
        • Use DSBulk to load data
        • Use Data Loader in Astra Portal
        • Monitor your databases
        • Manage multiple keyspaces
        • Using multiple regions
        • Terminate your database
        • Resize your classic database
        • Park your classic database
        • Unpark your classic database
      • Managing with DevOps API
        • Managing database lifecycle
        • Managing roles
        • Managing users
        • Managing tokens
        • Managing multiple regions
        • Get private endpoints
        • AWS PrivateLink
        • Azure PrivateLink
        • GCP Private Service
    • Astra CLI
    • Developing with Stargate APIs
      • Develop with REST
      • Develop with Document
      • Develop with GraphQL
        • Develop with GraphQL (CQL-first)
        • Develop with GraphQL (Schema-first)
      • Develop with gRPC
        • gRPC Rust client
        • gRPC Go client
        • gRPC Node.js client
        • gRPC Java client
      • Develop with CQL
      • Tooling Resources
      • Node.js Document API client
      • Node.js REST API client
    • Stargate QuickStarts
      • Document API QuickStart
      • REST API QuickStart
      • GraphQL API CQL-first QuickStart
    • API References
      • DevOps REST API v2
      • Stargate Document API v2
      • Stargate REST API v2

Retrieve data

Let’s check that the data was inserted. Use the query book with the primary key to find a book based on its title. Use http://localhost:8080/graphql/library to execute the query in GraphQL playground:

  • graphQL command

  • Result

query fetchBook {
  book(title: "Native Son") {
    title
    author
  }
}
{
  "data": {
    "book": [
      {
        "title": "Native Son",
        "author": "Richard Wright"
      }
    ]
  }
}

It is also possible to find books with a partial primary key. If more than one book has the same title (partition key), for instance, but different isbn codes (clustering key), then a listing of all books that match can be retrieved:

  • graphQL command

  • Result

query fetchSameBook {
  bookByTitle(title: "Groundswell") {
    title
    isbn
    author
  }
}
{
  "data": {
    "bookByTitle": [
      {
        "title": "Groundswell",
        "isbn": "978-1422125007",
        "author": [
          "Charlene Li"
        ]
      },
      {
        "title": "Groundswell",
        "isbn": "978-1439183595",
        "author": [
          "Katie Lee"
        ]
      }
    ]
  }
}}

In both queries, the title, isbn, and author are specified as return results.

To display the contents of a UDT, notice the inclusion of addresses and its embedded fields in the values designated as return values in this query to retrieve the readers:

  • graphQL command

  • Result

query fetchJane {
  readerByNameAndUserid(
      name: "Jane Doe",
      user_id: "f02e2894-db48-4347-8360-34f28f958590"
    ) {
    name
    user_id
    birthdate
    email
    address {
      street
      city
      state
      zipCode
    }
    reviews {
      bookTitle
      comment
      rating
      reviewDate
    }
  }
}
query fetchHerman {
  readerByNameAndUserid(
    name: "Herman Melville"
    user_id: "e0ec47e1-2b46-41ad-961c-70e6de629810"
  ) {
    name
    user_id
    birthdate
    email
    address {
      street
      city
      state
      zipCode
    }
  }
}
{
  "data": {
    "reader": [
      {
        "name": "Jane Doe",
        "user_id": "f02e2894-db48-4347-8360-34f28f958590",
        "birthdate": null,
        "email": [],
        "address": [],
        "reviews": [
          {
            "bookTitle": "Moby Dick",
            "comment": "It's about a whale.",
            "rating": 3,
            "reviewDate": "2021-04-01"
          },
          {
            "bookTitle": "Native Son",
            "comment": "An awesome work of art.",
            "rating": 5,
            "reviewDate": "2021-01-01"
          }
        ]
      }
    ]
  }
}

{
  "data": {
    "reader": [
      {
        "name": "Herman Melville",
        "user_id": "e0ec47e1-2b46-41ad-961c-70e6de629810",
        "birthdate": "1900-01-01",
        "email": [
          "herman.melville@gmail.com",
          "hermy@mobydick.org"
        ],
        "address": [
          {
            "street": "100 Main St",
            "city": "Boston",
            "state": "MA",
            "zipCode": "50050"
          }
        ]
      }
    ]
  }
}

In this example, the primary key consists of both name and user_id, to distinguish readers who might have the same name.

Stargate only allows combinations of fields that have: * at most one indexed field in the query * if no indexed field is present, then all partition key fields must be present ** it’s possible to omit some or all of the clustering fields (in which case the query may return multiple rows

Filter options for reads

It’s possible to customize the CQL condition of each parameter with @cql_where with the following arguments:

  • field: the GraphQL field name to which the condition applies

  • predicate: the conditional predicate to use

The filters available are:

Predicate

GraphQL fields that can have condition applied

EQ (equal)

partition key, clustering key, regular indexed field

IN (within)

partition key, clustering key, regular indexed field

GT (greater than)

clustering key

GTE (greater than or equal to)

clustering key

LT (less than)

clustering key

LTE (less than or equal to)

clustering key

CONTAINS

regular indexed field that is a list and has an index target of VALUES

IN example, that finds the books that are listed in the supplied array:

  • graphQL command

  • Result

type Query {
  booksIn( 
    title: [String!] @cql_where(field: "title", predicate: IN)
  ): [Book]
}

query fetchBooksIn {
  booksIn(title:["Native Son", "Moby Dick"]) {
      title 
      author
  }
}
{
  "data": {
    "booksIn": [
      {
        "title": "Moby Dick",
        "author": "Herman Melville"
      },
      {
        "title": "Native Son",
        "author": "Richard Wright"
      }
    ]
  }
}

IN example with 2 partition keys, demonstrating that values for each can be supplied:

  • graphQL command

  • Result

type Query {
  libcoll(type: String!, lib_id: Int!): [LibCollection]
  libCollIn( 
    type: [String!] @cql_where(field: "type", predicate: IN)
    lib_id: [Int!] @cql_where(field: "lib_id", predicate: IN)
  ): [LibCollection]
}

query fetchLibCollIn {
  libCollIn(type:[ "photo", "book" ], lib_id: [ 12345, 12543 ]) {
      type 
      lib_id
      lib_name
  }
}
{
  "data": {
    "libCollIn": [
      {
        "type": "book",
        "lib_id": 12345,
        "lib_name": "CSRM"
      },
      {
        "type": "photo",
        "lib_id": 12345,
        "lib_name": "CSRM"
      },
      {
        "type": "photo",
        "lib_id": 12543,
        "lib_name": "West Sacramento Historical Society"
      }
    ]
  }
}

GT example, that looks for equality on the partition key, and then a range of possible values for the clustering field:

  • graphQL command

  • Result

type Query {
  readerGT( 
    name: [String!] 
    user_id: [Uuid!]
    @cql_where(field: "user_id", predicate: GT)
  ): [Reader]
}

query fetchReadersGT {
  readerGT( name:"Herman Melville",  user_id: "e0ec47e1-2b46-41ad-961c-70e6de629800" ) {
    name
    user_id
    birthdate
  }
}
{
  "data": {
    "readerGT": [
      {
        "name": "Herman Melville",
        "user_id": "e0ec47e1-2b46-41ad-961c-70e6de629810",
        "birthdate": "1900-01-01"
      }
    ]
  }
}

GT example #2, that looks for a book with a title and an isbn code that is greater than the value supplied:

  • graphQL command

  • Result

type Query {
bookGT(
  title: String
  isbn: String @cql_where(field: "isbn", predicate: GT)
): [Book]
}

# retrieves only one book, by Katie Lee
query fetchIsbnGT {
  bookGT(title: "Groundswell", isbn: "978-1422125007") {
    title
    isbn
    author
  }
}
{
  "data": {
    "bookGT": [
      {
        "title": "Groundswell",
        "isbn": "978-1439183595",
        "author": [
          "Katie Lee"
        ]
      }
    ]
  }
}

LT example, that looks for a book with the same title and an isbn code that is less than the value supplied:

  • graphQL command

  • Result

type Query {
bookLT(
  title: String
  isbn: String @cql_where(field: "isbn", predicate: LT)
): [Book]
}

# retrieves only one book, by Charlene Li
query fetchIsbnLT {
  bookLT(title: "Groundswell", isbn: "978-1422125008") {
    title
    isbn
    author
  }
}
{
  "data": {
    "bookLT": [
      {
        "title": "Groundswell",
        "isbn": "978-1422125007",
        "author": [
          "Charlene Li"
        ]
      }
    ]
  }
}

CONTAINS example that shows how to retrieve a reader that submitted a supplied review:

  • graphQL command

  • Result

type Query {
  readerCONTAINS(
    reviews: ReviewInput! @cql_where(field: "reviews", predicate: CONTAINS)
  ): [Reader]
}

query fetchReadersCONTAINS {
  readerCONTAINS( reviews: {
          bookTitle: "Moby Dick"
          comment: "It's about a whale."
          rating: 3
          reviewDate: "2021-04-01"
        } ) {
    name
    user_id
    birthdate
  }
}
{
  "data": {
    "readerCONTAINS": [
      {
        "name": "Jane Doe",
        "user_id": "f02e2894-db48-4347-8360-34f28f958590",
        "birthdate": null
      }
    ]
  }
}

General Inquiries: +1 (650) 389-6000 info@datastax.com

© DataStax | Privacy policy | Terms of use

DataStax, Titan, and TitanDB are registered trademarks of DataStax, Inc. and its subsidiaries in the United States and/or other countries.

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries.

Kubernetes is the registered trademark of the Linux Foundation.

landing_page landingpage