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_urlis 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 --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":{}}'

The response body to the above request 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"
            }
        ]
    }
}

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 --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"}}'
{
    "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 --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"}}}'

And the result of our request is:

{
    "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 --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}}}'

And the result of our request is:

{
    "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 --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"}}'

And your body response will be:

{
    "data": {
        "deleteProduct": "Document Deleted"
    }
}

Last updated