Skip to content

Interacting With The API

Rhys Bartels-Waller edited this page May 4, 2020 · 8 revisions

Interacting with the API

The API is served over TCP, leveraging the existing infrastructure of HTTP. GraphQL adds a powerful query layer to give you complete control over the request, including the shape of the data in the response. Detailed below are some approaches for connecting and interacting with the API, however there may be other ways more suited for your use-case.

Setup up a local stack using docker-compose, or use a public deployment.

Command Line

graphqurl from the Hasura team is

curl for GraphQL with autocomplete, subscriptions and GraphiQL. Also a dead-simple universal javascript GraphQL client.

Or just use curl with a known query

➜  curl -X POST \
-H "Content-Type: application/json" \
-d '{"query": "{ cardano { blockHeight }}"}' \
http://localhost:3100/graphql  

{"data":{"cardano":{"blockHeight":70205}}}

Request within app

const query = `
  query getTransactions(
    $limit: Int,
    $offset: Int,
    $where: Transaction_bool_exp
  ) {
     transactions(
      limit: $limit,
      offset: $offset,
      where: $where
    ) {
      fee
      block {
        number
      }
      id
    }
  }`

const limit = 100
const offset = 2
const where = {
  block: {
    number: {
      _gte: 50
      _lt: 100
    }
  }
}

fetch('/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query,
    variables: { limit, offset, where },
  })
})
  .then(r => r.json())
  .then(data => console.log(`Page ${offset} containing ${limit} transactions:', data))

Stateful client

A full example application will demonstrate this.