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:

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.

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.

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:

await graphql(`
  query {
    location(locationId: "961546") {
      name
    }
  }
`);

More queries

Get a list of locations

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/

query($locationId: String!) {
  location(locationId: $locationId) {
    units(first: 10) {
      list {
        unitAddress {
          did
        }
      }
    }
  }
}

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

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.

mutation($locationId: String!, $did: String!) {
  location(locationId: $locationId) {
    provision(device: { did: $did }) {
      deployState
      unit {
        name
      }
    }
  }
}
Back to top