Read Group via Tags

Description

In this example we are showing you how you can use the GraphQL API to read a list of devices and their current measurement values by providing a Tag that has been assigned to that device.
This is what you need:
    Datacake Account
    GraphQL Library or Node-RED with GraphQL Extension

Node-RED

We are running the GraphQL Query inside a Node-RED Instance using a special GraphQL Extension for Node-RED. Basically, any GraphQL Client can be used but we prefer the graphical editor of Node-RED.

Preparations

Install Node-RED Extension

For the following tutorial we are using a Node-RED GraphQL Extension. So please make sure you install the following extension into your Node-RED:

Create Access Token

To access the devices in your Workspace you need to create an API Token. So head over into the members section of your Workspace and create an access token with the following right:

Assign Tags

In this tutorial, we will read measurement data from your devices based on the assigned tags. So we group devices with the help of the tag function.
Now, you have to assign a corresponding tag to these respective devices.
To do this, open the configuration of your devices and scroll down in the "General Settings" to the Tags item.
Here you assign a corresponding tag.
Separate Tags by entering a simple comma ",". You can assign several tags per device and tags can be shared between devices.

Show Tags on Fleet

In the fleet overview, you can configure the table to display all tags of your devices on the table.

Add GraphQL Node

In your Node-Red instance, now create a new flow and drag the GraphQL node onto the still empty flow.

Add GraphQL Config

Before we can now create the actual query, a GraphQL configuration must be stored. Select "Add new graphql-config" from the drop down menu and press on the "edit" icon at the right end of that drop down menu.
Please adjust the GraphQL configuration as follows:

Name

Simply enter a Name for these Settings. Can be anything you want as this is just used as a reference.

Endpoint

Here you need to specify the Datacake GraphQL endpoint for accessing our API, which is:
    https://api.datacake.co/graphql/
Please enter the following in the header field:
    Authorization

Authorization

In this field, type the word Token (plus space) followed by your API Access Token, which you created in the API Members Section in the steps before. The format is then as follows:
    Token d2efd2e4bdead193a647beef895d8fdead75be8c

Create Query

Double-clicking on the GraphQL query node opens the settings menu, through which you set the parameters for the query.

Writing the Query

Using the GraphQL API, you can write your own queries that will then return the data the way you would like it formatted.
This is a powerful feature for customization to your own apps or front ends. We will now use this function to write a query that returns all devices and the corresponding current readings.
This query looks like this:
1
query myQuery {
2
allDevices(inWorkspace:"9a829828-b962-4c8a-a4d6-68f94d96e356", searchTag:"group-a") {
3
id
4
currentMeasurements {
5
id
6
field {
7
fieldName
8
}
9
value
10
}
11
}
12
}
Copied!

Change Workspace ID

Please make sure that you replace the following section in the query with your own workspace ID of your Datacake workspace where the devices are located.
    inWorkspace:"9a829828-b962-4c8a-a4d6-68f94d96e356"

Where do I find my Workspace ID?

You can find the ID of your workspace in the workspace settings, which you can find via the sidebars and by clicking on the item "Workspace".
Clicking the "Copy" button copies the current ID to the clipboard for you. You can then paste it into the query.

Change Tag

In the GraphQL query, the respective tag has also been passed as a string. You must also modify this accordingly to the tag of your devices. To do this, adapt the following part.
    searchTag:"group-a"

Run Query

To execute the query, it is enough to use a simple "Inject" node, and connect it to the input of the GraphQL node. Also drag a debug node onto your flow and connect it to the first output of the GraphQL query.
If you press the inject node now, you should see the output of the query already in the flow.
Last modified 8mo ago