Insert data
Any of the created APIs can be used to interact with the GraphQL data, to write or read data.
First, let’s navigate to your new keyspace library
inside the playground.
Change the location to
http://localhost:8080/graphql/library
and add a couple of books to the book
table:
# insert 2 books in one mutation
mutation insert2Books {
moby: insertbook(value: {title:"Moby Dick", author:"Herman Melville"}) {
value {
title
}
}
catch22: insertbook(value: {title:"Catch-22", author:"Joseph Heller"}) {
value {
title
}
}
}
{
"data": {
"moby": {
"value": {
"title": "Moby Dick"
}
},
"catch22": {
"value": {
"title": "Catch-22"
}
}
}
}
Note that the keyword value
is used twice in the mutation.
The first use defines the value that the record is set to, for instance, the title
to Moby Dick and the author to Herman Melville.
The second use defines the values that will be displayed after the success
of the mutation, so that proper insertion can be verified.
This same method is valid for updates and read queries.
Insertion options
Three insertion options are configurable during data insertion or updating:
An example insertion that sets the consistency level and TTL:
# insert a book and set the option for consistency level
mutation insertBookWithOption {
nativeson: insertbook(value: {title:"Native Son", author:"Richard Wright"}, options: {consistency: LOCAL_QUORUM, ttl:86400}) {
value {
title
}
}
}
{
"data": {
"moby": {
"value": {
"title": "Moby Dick"
}
}
}
}
The serial consistency can also be set with serialConsistency
in the options,
if needed.
Insert collections (set, list, map)
Inserting a collection is simple. An example of inserting a list:
# insert an article USING A LIST (authors)
mutation insertArticle {
magarticle: insertarticle(value: {title:"How to use GraphQL", authors: ["First author", "Second author"], mtitle:"Database Magazine"}) {
value {
title
mtitle
authors
}
}
}
{
"data": {
"magarticle": {
"value": {
"title": "How to use GraphQL",
"mtitle": "Database Magazine",
"authors": [
"First author",
"Second author"
]
}
}
}
}
A map is slightly more complex:
mutation insertOneBadge {
gold: insertBadges(value: { btype:"Gold", earned: "2020-11-20", category: ["Editor", "Writer"] } ) {
value {
btype
earned
category
}
}
}
{
"data": {
"gold": {
"value": {
"badge_type": "Gold",
"badge_id": 100,
"earned": [
{
"key": "Writer",
"value": "2020-11-20"
}
]
}
}
}
}
Insert a tuple
Inserting a tuple involves inserting an object; note the use of item0
, item`1
,
and so on, to insert the parts of the tuple
# insert a reader record that uses a TUPLE
mutation insertJaneWithTuple{
jane: insertreader(
value: {
user_id: "b5b5666b-2a37-4d0b-a5eb-053e54fc242b"
name: "Jane Doe"
birthdate: "2000-01-01"
email: ["janedoe@gmail.com", "janedoe@yahoo.com"]
reviews: { item0: "Moby Dick", item1: 5, item2: "2020-12-01" }
}
) {
value {
user_id
name
birthdate
reviews {
item0
item1
item2
}
}
}
}
{
"data": {
"jane": {
"value": {
"user_id": "b5b5666b-2a37-4d0b-a5eb-053e54fc242b",
"name": "Jane Doe",
"birthdate": "2000-01-01",
"reviews": {
"item0": "Moby Dick",
"item1": 5,
"item2": "2020-12-01"
}
}
}
}
}
Insert a user-defined type (UDT)
Inserting a UDT requires taking careful note of the brackets used:
# insert a reader record that uses a UDT
mutation insertReaderWithUDT{
ag: insertreader(
value: {
user_id: "e0ed81c3-0826-473e-be05-7de4b4592f64"
name: "Allen Ginsberg"
birthdate: "1926-06-03"
addresses: [{ street: "Haight St", city: "San Francisco", zip: "94016" }]
}
) {
value {
user_id
name
birthdate
addresses {
street
city
zip
}
}
}
}
{
"data": {
"ag": {
"value": {
"user_id": "e0ed81c3-0826-473e-be05-7de4b4592f64",
"name": "Allen Ginsberg",
"birthdate": "1926-06-03",
"addresses": [
{
"street": "Haight St",
"city": "San Francisco",
"zip": "94016"
}
]
}
}
}
}