Skip to content

GraphQL examples and snippets

These are examples of queries which can be used regardless of the method of connecting to the GraphQL API.

Grabbing a sessionId

To quickly get a sessionId using you can use curl:

Bash
1
curl -d '{"username":"mattias@yanzi.se","password":"hunter2"}' https://api.yanzi.se/login

or the browser browsers bultin Fetch API. This can be directly pasted into the console of your web browser.

JavaScript
1
2
3
4
5
6
await fetch("https://api.yanzi.se/login", {
  method: "POST",
  body: JSON.stringify({ username: "mattias@yanzi.se", password: "hunter2" })
})
  .then(res => res.json())
  .then(json => json.sessionId);

Using the browser Fetch API

One of the simples methods of talking to the GraphQL API is, again, using your browsers console. If you have another favourite way of sending HTTP requests, feel free to use that instead.

JavaScript
1
2
3
4
5
6
7
8
9
function graphql(query, variables = {}) {
  return fetch("https://api.yanzi.se/graphql", {
    method: "POST",
    headers: {
      Authorization: "bearer YW5kcmVhcy5tYXJ0ZW..."
    },
    body: JSON.stringify({ query, variables: variables })
  }).then(r => r.json());
}

To use this snippet replace the sessionId with the one you just got, and then paste it into the browser console. You might have to change the host to something that makes sense in your situation as well. To actually send a request we can write something like below:

JavaScript
1
2
3
4
5
6
7
await graphql(`
  query {
    location(locationId: "961546") {
      name
    }
  }
`);

More queries

Get a list of locations

Text Only
1
2
3
4
5
6
7
8
query {
  locations {
    list {
      name
      locationId
    }
  }
}

Get a list of the first 10 dids found on a location

This example also uses variables in the query. If that seems scary you might find some comfort here: https://graphql.org/learn/

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
query($locationId: String!) {
  location(locationId: $locationId) {
    units(first: 10) {
      list {
        unitAddress {
          did
        }
      }
    }
  }
}

Get 10 conference rooms and their temperatures

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  location(locationId: "123456") {
    units(first:10, filter: {name:unitTypeFixed type:equals value:"conferenceRoom"}) {
      list {
        name
        unitAddress { did }
        latest(variableName:temperatureC) {
          ... on SampleTemp {
            temperature
          }
          time
        }
      }
    }
  }
}

Get a list of discovered devices and log a few of them

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
query($locationId: String!) {
  location(locationId: $locationId) {
    discoveredDevices {
      did
      macAddr
      productType
      state
    }
  }
}

Provision a device

Note

This input performs a change to your system when executed.

This is a mutation, so make sure you are sending your requests somewhere non critical.

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
mutation($locationId: String!, $did: String!) {
  location(locationId: $locationId) {
    provision(device: { did: $did }) {
      deployState
      unit {
        name
      }
    }
  }
}