Aptos GraphQL Server

Aptos GraphQL Server

Description:

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

First of all, I consider it as a tool that will allow you to send data to one request from two sources at once - API and Indexer, without thinking about their implementation.

Goals

Provide users with a tool with which they could receive information about the state of the blockchain, addresses, transactions, tokens, and other objects without thinking about the intricacies of implementing various sources of information such as API and Indexer (in the future, additional sources with their own implementation may appear) in a simple, single accessible format.

Milestones

First alpha demo in next few days (4-5)
First public beta ~30 days
First release ~45 days

You can see the live demo:

You can use this demo query:

{
  hello
  health(node: "https://fullnode.aptos-node.net") {
    node
    status
  }
  ledger(node: "https://fullnode.devnet.aptoslabs.com") {
    node
    state {
      ledger_version
      epoch
      chain_id
      ledger_timestamp,
      oldest_ledger_version,
      node_role,
      message
    }
  }
}

Skills & Experience

All my team - I’m! I have been programming for about 30 years. For the last 10 years, I have given a very significant role to web programming.

My general skills:

  • JavaScript, NodeJS
  • HTML
  • CSS/LESS
  • PHP
  • SQL, PL/SQL, PSQL (MySQL, Oracle, Postgres)

Motivation

I really like the concept of Aptos, its possibilities, and prospects. At the moment, I have practically withdrawn from other projects in order to be able to concentrate on Aptos. I’m currently heavily learning Rust and Move to get to the level of working with the Aptos kernel.

Also, my age plays a big role, unfortunately, I am already 50, and it is very difficult for me to find a job because they usually hire young people. I am currently unemployed and hope that my ecosystem projects and community projects will allow me to earn money to support my family.

Technical Architecture

Aptos GraphQL Server written on:

  • Server-side - NodeJS,
  • Aptos API @olton/apos-api
  • Datetime library @olton/datetime
  • GraphQL and Express libs

Key Risks & Challenges

The project does not carry risks, because it performs only an informative role and does not affect the data inside the blockchain in any way at the first stage.

8 Likes

This project consists of 3 separate projects: Aptos API, Aptos Indexer API, and Aptos GraphQL. Below are some examples of how this works.

Demo scene https://graphql.aptos-node.net

Demo queries:

  1. Hello world
{ hello }
  1. Get Nodes health
{
  health(nodes: [
    "https://fullnode.aptos-node.net",
    "https://fullnode.devnet.aptoslabs.com",
  ]) {
    node
    status
  }
}
  1. Get ledger state from two different nodes
 {
  ledger(nodes: [
    "https://fullnode.aptos-node.net",
    "https://fullnode.devnet.aptoslabs.com"
  ]) {
    node
    state {
      chain_id
      epoch
      ledger_version
      ledger_timestamp
      node_role
      oldest_ledger_version
      message
    }
  }
}
  1. Get address info
{
  address(addr: ["0x310dfd70948d6b22c5e6a573719e21f0437d4fa4986a106e439a1f9d44dcae0c"]){
    address
    authentication_key
    sequence_number
  }
}
  1. Get Address Infor and address balance
{
  address(addr: ["0x310dfd70948d6b22c5e6a573719e21f0437d4fa4986a106e439a1f9d44dcae0c"]){
    address
    authentication_key
    sequence_number
    balance(coin: "0x1::aptos_coin::AptosCoin") {
      coin
      balance
    }
  }
}
1 Like

Hi Olton, I hope that you are doing well. I am sorry to hear about your difficulties finding a career, maybe your success here will generate opportunities for yourself in the Aptos ecosystem!

I had a few questions relating to your experience in Move. How you are enjoying Move? Has the transition been easy for you? Are there any areas in which you feel the guides and support are lacking?

I also recommend checking out the Aptos grant program. I am excited to test the beta of your project next month!

How to use Aptos GraphQL Server

NodeJS

const GRAPHQL_ENDPOINT = 'https://graphql.aptos-node.net'

async function fetchGraphQL(endpoint, query, variables = {}) {
    try {
        const result = await fetch(
            `${endpoint}`,
            {
                method: "POST",
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    query,
                    variables,
                })
            }
        )

        return result.ok ? await result.json() : null
    } catch (e) {
        console.error("The Request to GraphQL war aborted! " + e.name + " " + e.message)
        return null
    }
}

const query = /* GraphQL */`
    query{
        hello
    }
`

const response = await fetchGraphQL(GRAPHQL_ENDPOINT, query)

console.log(response)

Output

{
  "data": {
    "hello": "Hello from Aptos GraphQL Server!"
  }
}
1 Like

An example with data from both sources - API and Indexer:

{
  ledger{
    node
    state {
      chain_id
      epoch
      ledger_version
      ledger_timestamp
      node_role
      oldest_ledger_version
      message
    }
  }
  transactionsCount{
    success
    failed
    unknown
    state_checkpoint_transaction
    block_metadata_transaction
    user_transaction
    genesis_transaction
  }
}

You can try it in the sandbox https://graphql.aptos-node.net

2 Likes

With this request, you can request balances for several coins

{
  address(addr: "0x310dfd70948d6b22c5e6a573719e21f0437d4fa4986a106e439a1f9d44dcae0c"){
    address
    aptos_coin: balance(coin: "0x1::aptos_coin::AptosCoin"){
      coin
      balance
    }
    moon_coin: balance(coin: "0x1::moon_coin::MoonCoin"){
      coin
      balance
    }
  }
}

1 Like

Transactions

Retrieving information about a transaction short info:

{
  transaction(hash: "0xb8885126369e72e9b33d3bbb2600df592ea44b372473b7cf725602101f0bbbd4"){
    type
    version
    hash
    success
    vm_status
    gas_used
    timestamp
  }
}

Detailed info:

{
  transaction(hash: "0xb8885126369e72e9b33d3bbb2600df592ea44b372473b7cf725602101f0bbbd4"){
    detail
  }
}

Mutation on faucet example

Most discussions of GraphQL focus on data fetching, but any complete data platform needs a way to modify server-side data as well.

In GraphQL any operations that cause write should be sent explicitly via a mutation

Just like in queries, if the mutation field returns an object type, you can ask for nested fields. This can be useful for fetching the new state of an object after an update. For example, we will use a faucet operation.

Let’s look at a simple example mutation:

You can use my GraphQL sandbox 0n https://graphql.aptos-node.net

And the same example on NodeJS:

import fetchGraphQL, {GRAPHQL_ENDPOINT, GRAPHQL_ENDPOINT_LOCAL} from "../src/graphql/fetcher/fetcher.js";
import {logObject} from "../src/helpers/logging.js";

const mutation = /* GraphQL */ `
    mutation faucet ($addr: String!, $amount: Int!) {
        faucet(addr: $addr, amount: $amount){
            address
            coin
            balance
        }
    }
`

const faucet = await fetchGraphQL(GRAPHQL_ENDPOINT_LOCAL, mutation, {
    addr: "0x310dfd70948d6b22c5e6a573719e21f0437d4fa4986a106e439a1f9d44dcae0c",
    amount: 111
})

This is the query I created to get all mint transactions for a specified address

{
  address(addr: "0x310dfd70948d6b22c5e6a573719e21f0437d4fa4986a106e439a1f9d44dcae0c"){
    minting{
        type
        version
        hash
        success
        vm_status
        gas_used
        timestamp
        amount
        coin
    }
  }
}

Retrieving information about address rounds and proposals

Address Rounds:

{
    address(addr: "0xc1452c6f8dc0cf6b6289872c7a885e7b4a4d673074186510da5499390297c7ca"){
        rounds{
            epoch
            rounds
        }
    }
}

Address Proposals (validator votes):

{
    address(addr: "0xc1452c6f8dc0cf6b6289872c7a885e7b4a4d673074186510da5499390297c7ca"){
        proposal{
            type
           version
           hash
           success
          vm_status
          timestamp
          id
          round
          epoch
          detail
        }
    }
}

Mutation for creating account

mutation{
    createAccount{
        address
        publicKey
        authKey
        privateKey
    }
}

Mutation - Sending Coins

To send coins, you can use mutation below, result contains detailed report

mutation send_coins ($pk: String!, $rs: String!, $am: String!){
    sendCoins(
        privateKey: $pk
        receiver: $rs
        amount: $am
    ) {
        coin
        balance
        amount
        gas{
            gas_used
            unit_price
            max_gas
        }
    }
}

With arguments (privateKey for sender, address for receiver, and amount)

{
    "pk": "772049f3272f8e29...2fed5ffece089bd75414d71f",
    "rs": "0xa78b0131323.....a185cb323192058f31a8",
    "am": "100"
}

Hey, talking about your employment, have you posted on Twitter? If I remember correctly, dapptos and tsunami finance were hiring for dev roles.

1 Like

You can find all demos here

dear @olton
your work is awesome
you are a very smart person, your efforts should definitely be appreciated by the team, I like you and wish you success! you are well done!

1 Like