Adding rows in your table using GraphQL
Insert data in your tables using Astra’s GraphQL API.
After creating a table in your keyspace using GraphQL, you can add rows of data.
Prerequisites
Write 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.
Switch to the graphql
tab in the GraphQL playground, and change the location
to https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql/library
The main importance is changing the keyspace name, as the default is system
.
First, let’s 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
}
}
}
curl -sL --request POST --url http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql/library \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation insert2Books {\n moby: insertbook(value: {title:\"Moby Dick\", author:\"Herman Melville\"}) {\n value {\n title\n }\n }\n catch22: insertbook(value: {title:\"Catch-22\", author:\"Joseph Heller\"}) {\n value {\n title\n }\n }\n}","variables":{}}'
{
"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:
Note that in Astra, the consistency levels ANY
, ONE
, or LOCAL_ONE
cannot be used.
The default is LOCAL_QUORUM
.
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
}
}
}
curl -sL --request POST --url http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql/library \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation insertBookWithOption {\n nativeson: insertbook(value: {title:\"Native Son\", author:\"Richard Wright\"}, options: {consistency: LOCAL_QUORUM, ttl:86400}) {\n value {\n title\n }\n }\n}","variables":{}}'
{
"data": {
"nativeson": {
"value": {
"title": "Native Son"
}
}
}
}
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
}
}
}
curl -sL --request POST --url http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql/library \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation insertArticle {\n magarticle: insertarticle(value: {title:\"How to use GraphQL\", authors: [\"First author\", \"Second author\"], mtitle:\"Database Magazine\"}) {\n value {\n title\n mtitle\n authors\n }\n }\n}","variables":{}}'
{
"data": {
"magarticle": {
"value": {
"title": "How to use GraphQL",
"mtitle": "Database Magazine",
"authors": [
"First author",
"Second author"
]
}
}
}
}
A map is slightly more complex:
mutation insertOneBadge {
editor1: insertbadge(value: { user_id:100, badge_type: "Editor", badge_id: 100, earned: {key:"Gold", value:"2020-11-20"} } ) {
value {
user_id
badge_type
badge_id
earned
}
}
}
curl -sL --request POST --url http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql/library \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation insertOneBadge {\n editor1: insertbadge(value: { user_id: \"b5b5666b-2a37-4d0b-a5eb-053e54fc242b\", badge_type: \"Editor\", badge_id: 100, earned: {key:\"Gold\", value:\"2020-11-20\"} } ) {\n value {\n user_id\n badge_type\n badge_id\n earned {\n key\n value\n }\n }\n }\n}","variables":{}}'
{
"data": {
"editor1": {
"value": {
"user_id": "b5b5666b-2a37-4d0b-a5eb-053e54fc242b",
"badge_type": "Editor",
"badge_id": 100,
"earned": [
{
"key": "Gold",
"value": "2020-11-20"
}
]
}
}
}
}
Insert a tuple
Inserting a tuple involves inserting an object; note the use of item0
, item1
,
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
}
}
}
}
curl -sL --request POST --url http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql/library \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"query":"# insert a reader record that uses a TUPLE\nmutation insertJaneWithTuple{\n jane: insertreader(\n value: {\n user_id: \"b5b5666b-2a37-4d0b-a5eb-053e54fc242b\"\n name: \"Jane Doe\"\n birthdate: \"2000-01-01\"\n email: [\"janedoe@gmail.com\", \"janedoe@yahoo.com\"]\n reviews: { item0: \"Moby Dick\", item1: 5, item2: \"2020-12-01\" }\n }\n ) {\n value {\n user_id\n name\n birthdate\n reviews {\n item0\n item1\n item2\n }\n }\n }\n}","variables":{}}'
{
"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
}
}
}
}
curl -sL --request POST --url http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql/library \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation insertReaderWithUDT{\n ag: insertreader(\n value: {\n user_id: \"e0ed81c3-0826-473e-be05-7de4b4592f64\"\n name: \"Allen Ginsberg\"\n birthdate: \"1926-06-03\"\n addresses: [{ street: \"Haight St\", city: \"San Francisco\", zip: \"94016\" }]\n }\n ) {\n value {\n user_id\n name\n birthdate\n addresses {\n street\n city\n zip\n }\n }\n }\n }","variables":{}}'
{
"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"
}
]
}
}
}
}