Comment on page
Using GraphQL
In this tutorial we will show you how you can use the powerful GraphQL API to access historical and current device data with Datacake.
You can use a cURL Command to call the GraphQL Library directly.
curl \
-X GET \
-H "Content-Type: application/json" \
-H "Authorization: Token 1234567yourtoken1234567" \
--data '{"query": "query { allDevices(inWorkspace:\"dc578994-your-workspace-84221d0\") { currentMeasurements(allActiveFields: true) { value field { verboseFieldName fieldName } } } }"}' \
https://api.datacake.co/graphql/
You can use the Apollo GraphQL Library in your iOS / Swift Applications:
You can use a function node in Node-RED and create a custom GraphlQL query that forwards data directly into Node-RED as a parsed JSON-Object.
// Datacake API Token and Workspace
var token = "123456mytoken123456";
var workspace = "1234myworkspace1234";
// GraphQL API URL
msg.url = "https://api.datacake.co/graphql";
// GraphQL Header
msg.headers = {
"Authorization": "Token "+token,
"Content-Type": "application/json"
};
// GraphQL Query
var query = `query {
allDevices(inWorkspace:${workspace}) {
id
serialNumber
verboseName
currentMeasurements(allActiveFields: true) {
field {
fieldName
verboseFieldName
}
value
}
}
}`;
msg.payload = {"query": query};
msg.body = {"query": query};
return msg;
Copy the following Node-RED Flow of a working Datacake GraphQL Example and paste this into your Node-RED using "Import" function.
query {
allWorkspaces {
id
name
}
}
query {
allDevices(inWorkspace:"f6331019-8978-4a86-b1bc-3522546f67d5") {
id
verboseName
}
}
You can use the
currentMeasurements
option to have all the last recorded measurements of one or more devices returned.Use the
allActiveFields
option as follows to return only those fields that are active in the database.query {
allDevices(inWorkspace:"f6331019-8978-4a86-b1bc-3522546f67d5") {
currentMeasurements(allActiveFields: true) {
value
modified
field {
verboseFieldName
fieldName
}
}
}
}
query {
allDevices(inWorkspace:"f6331019-8978-4a86-b1bc-3522546f67d5") {
currentMeasurements(fieldNames:["BATTERY"]) {
value
modified
field {
verboseFieldName
fieldName
}
}
}
}
Instead of providing an Array of Field Identifiers you can use the
currentMeasurement
Option to Query a specific field only.query {
allDevices(inWorkspace:"f6331019-8978-4a86-b1bc-3522546f67d5") {
currentMeasurement(fieldName:"BATTERY") {
value
modified
field {
verboseFieldName
fieldName
}
}
}
}
The above examples are using the
allDevices
Option to query all devices in a given Workspace. If you just want to access data from a specific device only, you can use the device
Option.device(deviceId:"faf34567-5f13-4536-b3f3-a4a1e245ab2a")
Use this as in the following example query:
query {
device(deviceId:"faf34567-5f13-4536-b3f3-a4a1e245ab2a") {
currentMeasurements(allActiveFields: true) {
value
modified
field {
verboseFieldName
fieldName
}
}
}
}
If your fields in database store a
String
-type value you have to add the valueString
option to return the corresponding value.query {
allDevices(inWorkspace:"f6331019-8978-4a86-b1bc-3522546f67d5") {
currentMeasurements(allActiveFields: true) {
valueString
modified
field {
verboseFieldName
fieldName
}
}
}
}
maximum(timeRangeStart:"2021-04-16T00:00", timeRangeEnd:"2021-04-17T00:00")
average(timeRangeStart:"2021-04-16T00:00", timeRangeEnd:"2021-04-17T00:00")
change(average(timeRangeStart:"2021-04-16T00:00", timeRangeEnd:"2021-04-17T00:00")
query {
device(deviceId:"faf34567-5f13-4536-b3f3-a4a1e245ab2a") {
currentMeasurements(allActiveFields: true) {
value
maximum(timeRangeStart:"2021-04-16T00:00", timeRangeEnd:"2021-04-17T00:00")
field {
verboseFieldName
fieldName
}
}
}
}
So will look like
{
"data": {
"device": {
"currentMeasurements": [
{
"value": 3.067,
"average": 3.06697222222223,
"maximum": 3.069,
"change": 0,
"modified": "2021-04-17T12:58:30.554976+00:00",
"field": {
"verboseFieldName": "Battery",
"fieldName": "BATTERY"
}
}
]
}
}
}
query {
allDevices(inWorkspace:"f6331019-8978-4a86-b1bc-3522546f67d5") {
history(
fields:["BATTERY"],
timerangestart:"2021-04-16T00:00",
timerangeend:"2021-04-17T00:00",
resolution:"120m"
)
}
}
query {
device(deviceId:"faf34567-5f13-4536-b3f3-a4a1e245ab2a") {
history(fields:["BATTERY"], timerangestart:"2021-04-16T00:00", timerangeend:"2021-04-17T00:00", resolution:"raw")
}
}
Fields have to be Identifier
fields:["BATTERY"]
One of the options for the history settings is an option called resolution. This gives you two options:
raw
- Raw Data Export. Data will be exported as stored in the databaseXm
- Quantisation in X Minutes - Data will be combined into timerange-chunks of given value.
The operation for Quantisation is an average. You cannot change this right now.
query {
device(deviceId:"faf34567-5f13-4536-b3f3-a4a1e245ab2a") {
history(
fields:["BATTERY"],
timerangestart:"2021-04-16T00:00",
timerangeend:"2021-04-17T00:00",
resolution:"raw"
)
}
}
query {
device(deviceId:"faf34567-5f13-4536-b3f3-a4a1e245ab2a") {
history(
fields:["BATTERY"],
timerangestart:"2021-04-16T00:00",
timerangeend:"2021-04-17T00:00",
resolution:"20m"
)
}
}
The following snippet works for both
allDevices
and specific device
Options.query {
allDevices(inWorkspace:"f6331019-8978-4a86-b1bc-3522546f67d5") {
location
lastHeard
claimed
claimCode
tags
serialNumber
}
}
Last modified 4mo ago