Database APIs
GraphQL always uses the POST method and the same endpoint to make requests. Here, we are going to imagine we have a products table under a project, and we are going to create, read, update, and delete a product from this table using GraphQL.
The endpoint URL is as follows:
https://YOUR_PROJECT_ID.api.deskree.com/api/v1/graphql
You can access the GraphQL schema for your project in:
Note that the Users table is a special table and does not allow Creation or Deletion directly via GraphQL requests since account creation/removal is handled by AUTH APIs. For more information, view our Users Table guide.
The body query to get a list of products, including all of the objects' properties is as follows:
query products {
products {
uid
name
price
author
updatedAt
createdAt
}
}
And the body response to the request above is:
{
"data": {
"products": [
{
"uid": "0ynOfuBeph76aqH6MkBc",
"name": "Resee's",
"price": 0.99,
"author": "",
"updatedAt": "2022-07-04T16:01:09-04:00",
"createdAt": "2022-06-29T11:50:43-04:00"
},
{
"uid": "224oR81doLNtQzfDsGxy",
"name": "KitKat",
"price": 1.49,
"author": "",
"updatedAt": "2022-06-28T16:53:29-04:00",
"createdAt": "2022-06-28T16:53:29-04:00"
}
]
}
}
The beauty of GraphQL is that you can fetch only the necessary information just by changing the query structure. Here's another example of how to fetch a single product by UID, and this time let's just fetch the name and price properties of it:
query products {
products (uid: "0ynOfuBeph76aqH6MkBc") {
name
price
}
}
And the result for this new request is:
{
"data": {
"products": [
{
"name": "string",
"price": null
}
]
}
}
Now, let's create a product, and return this product's uid, name, and price. To do so, we can have a body mutation as follows:
mutation createProduct {
createProduct (data: { name: "Kopenhagen", price: 1.00 }) {
uid
name
price
}
}
And the result of our request is:
{
"data": {
"createProduct": {
"uid": "0ynOfuBeph76aqH6MkBc",
"name": "Kopenhagen",
"price": 1
}
}
}
Now, let's update the previous product price, and return the product's uid, name, and price. To modify a product, we need to specify which product to update, and the new value we want to set. To do so, we can have a body mutation as follows:
mutation updateProduct {
updateProduct("uid": "0ynOfuBeph76aqH6MkBc", data: { price: 3.50 }) {
uid
name
price
}
}
And the result of our request is:
{
"data": {
"createProduct": {
"uid": "0ynOfuBeph76aqH6MkBc",
"name": "Kopenhagen",
"price": 3.50
}
}
}
To delete a product, we just need to specify the uid of the object within the following mutation:
mutation deleteProduct {
deleteProduct (uid: "0ynOfuBeph76aqH6MkBc")
}
And your body response will be:
{
"data": {
"deleteProduct": "Document Deleted"
}
}
Last modified 4mo ago