GraphQL in Blockscout

What is GraphQL

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. It provides an efficient, powerful and flexible approach to developing web APIs. It allows clients to define the structure of the data required, and exactly the same structure of the data is returned from the server, therefore preventing excessively large amounts of data from being returned.

Key concepts of the GraphQL query language are:

  • Hierarchical

  • Strongly typed

  • Client-specified queries

Advantages of GraphQL:

  • Declarative integration on client (what data/operations do I need)

  • A standard way to expose data and operations

  • Support for real-time data (with subscriptions)

Query types

There are three main query types in GraphQL schema:

1) Query: fetch data

query {
  allPosts {
    description
    text
  }
}

2) Mutation: change data.

   mutation {
     updatePost(id: 1, text: "text") {
       text
     }
   }
  1. Subscription: subscribe to real-time data.

subscription {
  newPost(category: [1]) {
    description
    text
  }
}

Access GraphQL API

To access Blockscout GraphQL interface you can use GraphiQL - in-browser IDE for exploring GraphQL. It's built in to Blockscout. From the APIs dropdown menu choose GraphQL.

You can also use your favorite http client:

curl 'https://eth-sepolia.blockscout.com/graphiql'
  -H 'Authorization: Bearer YOUR_AUTH_TOKEN'
  -d '{"query":""{transaction(hash:\"0x4f3a80620613d18a5073108f56558c7cbf0020c8ccb1acbad2c1f1fe3714e5ce\"){blockNumbertoAddressHashfromAddressHashcreatedContractAddressHashvaluestatusnoncehasherrorgasgasPricegasUsedcumulativeGasUsedidindexinputrsv}}""}'

Queries

Blockscout's GraphQL API provides queries and a subscription. You can view them in the GraphQL interface in the Docs menu. Example Queries:

Example Query to retrieve transactions for a specific address

{
  address(hash: "0x...") {
    transactions(first:5) {
      edges {
        node {
          blockNumber
          createdContractAddressHash
          fromAddressHash
          gas
          hash
        }
      }
    }
  }
}

Note that transactions can accept the following arguments:

  • first

  • after

  • before

Last updated

Logo

Copyright © Blockscout Limited 2023-2024