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
      }
    ]
  }
}