Database APIs In GraphQL, requests always use the POST method and a single endpoint. Imagining a "products" table under a project, we can create, read, update, and delete a product using GraphQL.
base_url
is equal to https://${project_id}.api.deskree.com/api/v1/graphql
Query
The query to retrieve a list of products, including all object properties, is as follows:
cURL JavaScript
Copy curl --location 'https://${project_id}.api.deskree.com/api/v1/graphql' \
--header 'Authorization: Bearer ${idToken}' \
--header 'Content-Type: application/json' \
--data '{"query":"query products {\n products {\n uid\n name\n price\n author\n updatedAt\n createdAt\n }\n}","variables":{}}'
Copy var myHeaders = new Headers ();
myHeaders .append ( "Authorization" , "Bearer ${idToken}" );
myHeaders .append ( "Content-Type" , "application/json" );
var graphql = JSON .stringify ({
query: "query products {\n products {\n uid\n name\n price\n author\n updatedAt\n createdAt\n }\n}",
variables : {}
})
var requestOptions = {
method : 'POST' ,
headers : myHeaders ,
body : graphql ,
redirect : 'follow'
};
fetch ( "https://${project_id}.api.deskree.com/api/v1/graphql" , requestOptions)
.then (response => response .text ())
.then (result => console .log (result))
.catch (error => console .log ( 'error' , error));
The response body to the above request is:
Copy {
"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"
}
]
}
}
GraphQL's beauty lies in its ability to retrieve only the necessary information by altering the query structure. Here's another example of fetching a single product by UID, focusing on retrieving only the name and price properties
cURL JavaScript
Copy curl --location 'https://${project_id}.api.deskree.com/api/v1/graphql' \
--header 'Authorization: Bearer ${idToken}' \
--header 'Content-Type: application/json' \
--data '{"query":"query products($uid: ID) {\n products(uid: $uid) {\n name\n price\n }\n}","variables":{"uid":"0ynOfuBeph76aqH6MkBc"}}'
Copy var myHeaders = new Headers ();
myHeaders .append ( "Authorization" , "Bearer ${idToken}" );
myHeaders .append ( "Content-Type" , "application/json" );
var graphql = JSON .stringify ({
query : "query products($uid: ID) {\n products(uid: $uid) {\n name\n price\n }\n}" ,
variables : { "uid" : "0ynOfuBeph76aqH6MkBc" }
})
var requestOptions = {
method : 'POST' ,
headers : myHeaders ,
body : graphql ,
redirect : 'follow'
};
fetch ( "https://${project_id}.api.deskree.com/api/v1/graphql" , requestOptions)
.then (response => response .text ())
.then (result => console .log (result))
.catch (error => console .log ( 'error' , error));
Copy {
"data" : {
"products" : [
{
"name" : "string" ,
"price" : 0.99
}
]
}
}
Create Mutation
To create a product and return its UID, name, and price, use the following mutation body:
cURL JavaScript
Copy curl --location 'https://${project_id}.api.deskree.com/api/v1/graphql' \
--header 'Authorization: Bearer ${idToken}' \
--header 'Content-Type: application/json' \
--data '{"query":"mutation createproduct ($data: productInput!) {\n createproduct (data: $data) {\n uid\n price\n name\n }\n}","variables":{"data":{"price":1,"name":"Kopenhagen"}}}'
Copy var myHeaders = new Headers ();
myHeaders .append ( "Authorization" , "Bearer ${idToken}" );
myHeaders .append ( "Content-Type" , "application/json" );
var graphql = JSON .stringify ({
query: "mutation createproduct ($data: productInput!) {\n createproduct (data: $data) {\n uid\n price\n name\n }\n}",
variables : { "data" : { "price" : 1 , "name" : "Kopenhagen" }}
})
var requestOptions = {
method : 'POST' ,
headers : myHeaders ,
body : graphql ,
redirect : 'follow'
};
fetch ( "https://${project_id}.api.deskree.com/api/v1/graphql" , requestOptions)
.then (response => response .text ())
.then (result => console .log (result))
.catch (error => console .log ( 'error' , error));
And the result of our request is:
Copy {
"data" : {
"createProduct" : {
"uid" : "0ynOfuBeph76aqH6MkBc" ,
"name" : "Kopenhagen" ,
"price" : 1
}
}
}
Update Mutation
To update the previous product's price and return its UID, name, and updated price, use the following mutation body. When modifying a product, specify the product to update and the new value to set:
cURL JavaScript
Copy curl --location 'https://${project_id}.api.deskree.com/api/v1/graphql' \
--header 'Authorization: Bearer ${idToken}' \
--header 'Content-Type: application/json' \
--data '{"query":"mutation updateproduct ($uid: ID, $data: productInput!) {\n updateproduct (uid: $uid, data: $data) {\n uid\n createdAt\n author\n price\n name\n updatedAt\n }\n}","variables":{"uid":"0ynOfuBeph76aqH6MkBc","data":{"price":3.5}}}'
Copy var myHeaders = new Headers ();
myHeaders .append ( "Authorization" , "Bearer ${idToken}" );
myHeaders .append ( "Content-Type" , "application/json" );
var graphql = JSON .stringify ({
query: "mutation updateproduct ($uid: ID, $data: productInput!) {\n updateproduct (uid: $uid, data: $data) {\n uid\n createdAt\n author\n price\n name\n updatedAt\n }\n}",
variables : { "uid" : "0ynOfuBeph76aqH6MkBc" , "data" : { "price" : 3.5 }}
})
var requestOptions = {
method : 'POST' ,
headers : myHeaders ,
body : graphql ,
redirect : 'follow'
};
fetch ( "https://${project_id}.api.deskree.com/api/v1/graphql" , requestOptions)
.then (response => response .text ())
.then (result => console .log (result))
.catch (error => console .log ( 'error' , error));
And the result of our request is:
Copy {
"data" : {
"createProduct" : {
"uid" : "0ynOfuBeph76aqH6MkBc" ,
"name" : "Kopenhagen" ,
"price" : 3.50
}
}
}
Delete Mutation
To delete a product, specify the UID of the object in the following mutation:
cURL JavaScript
Copy curl --location 'https://${project_id}.api.deskree.com/api/v1/graphql' \
--header 'Authorization: Bearer ${idToken}' \
--header 'Content-Type: application/json' \
--data '{"query":"mutation deleteproduct ($uid: ID) {\n deleteproduct (uid: $uid)\n}","variables":{"uid":"0ynOfuBeph76aqH6MkBc"}}'
Copy var myHeaders = new Headers ();
myHeaders .append ( "Authorization" , "Bearer ${idToken}" );
myHeaders .append ( "Content-Type" , "application/json" );
var graphql = JSON .stringify ({
query : "mutation deleteproduct ($uid: ID) {\n deleteproduct (uid: $uid)\n}" ,
variables : { "uid" : "0ynOfuBeph76aqH6MkBc" }
})
var requestOptions = {
method : 'POST' ,
headers : myHeaders ,
body : graphql ,
redirect : 'follow'
};
fetch ( "https://${project_id}.api.deskree.com/api/v1/graphql" , requestOptions)
.then (response => response .text ())
.then (result => console .log (result))
.catch (error => console .log ( 'error' , error));
And your body response will be:
Copy {
"data" : {
"deleteProduct" : "Document Deleted"
}
}
Last updated 10 months ago