EIPs Explained

EIP-1767 · Stagnant · Interface · 2019-02-14

EIP-1767: GraphQL interface to Ethereum node data

Specifies a GraphQL schema and endpoint for Ethereum nodes, letting clients fetch exactly the block, transaction, account and log data they need in one query.

What problem does it solve?

The JSON-RPC interface has two kinds of weakness. It was loosely specified, so clients disagreed on details such as how to encode an empty byte string ("", "0x" or "0x0"). And it returns fixed bundles of fields, so the node often does work the caller does not need: eth_getBlock always fetches totalDifficulty, and fetching every receipt in a block via eth_getTransactionReceipt can mean deserializing the same data again and again.

EIP-1767 proposes GraphQL as a cleaner read API. The caller describes exactly which fields it wants, related data (block → transactions → receipts → logs) comes back in one round trip, and the schema itself is the formal specification.

How it works

  • Nodes must expose a GraphQL endpoint over HTTP, should serve it at /graphql, and should use port 8547 by default. They may serve the GraphiQL explorer at /.
  • The schema defines scalars (Bytes32, Address, Bytes, BigInt, Long) with strict hex encodings, and types such as Block, Transaction, Account, Log, CallResult, SyncState and Pending.
  • Top-level queries: block, blocks, transaction, logs, pending, gasPrice, protocolVersion, syncing.
  • One mutation: sendRawTransaction.
  • Calls and gas estimation are fields on a block (block { call(...) }), so they run against that block's state.

Most read-only JSON-RPC methods map directly onto queries:

JSON-RPC GraphQL
eth_blockNumber { block { number } }
eth_getBalance { block { account(address: "0x...") { balance } } }
eth_getTransactionReceipt { transaction(hash: "0x...") { status gasUsed logs { ... } } }
eth_sendRawTransaction mutation { sendRawTransaction(data: "0x...") }

Node-side signing (eth_sign, eth_sendTransaction), compilers, filters and mining calls are intentionally left out. Client-specific extensions must be prefixed, for example _geth_.

Sample code

All transactions in the latest block with their status and logs, in one request:

{
  block {
    number
    hash
    transactions {
      hash
      from { address }
      to { address }
      status
      gasUsed
      logs { topics data }
    }
  }
}

Sending it from TypeScript:

const query = `{ block { number transactions { hash status } } }`;

const res = await fetch("http://localhost:8547/graphql", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ query }),
});
const { data } = await res.json();
console.log(data.block.number, data.block.transactions.length);

Things to know

  • Status: Stagnant. The EIP itself was not finalized, though the spec lists implementations in go-ethereum (from 1.9.0) and Pantheon (now Hyperledger Besu).
  • Endpoints are usually disabled by default and must be turned on with a client flag. Check your client's documentation for the exact option and port.
  • Fields described in the spec reflect proof-of-work era blocks (difficulty, ommers, mixHash). Client schemas have evolved since; inspect the live schema with GraphQL introspection rather than relying on this EIP.
  • JSON-RPC (EIP-1474 and the current execution API spec) remains the primary interface; GraphQL is an optional extra.

#graphql #api #node #json-rpc #interface

Details

Authors
Nick Johnson (@arachnid), Raúl Kripalani (@raulk), Kris Shinn (@kshinn)
Status
Stagnant
Type
Standards Track / Interface
Created
2019-02-14

Machine-readable: Markdown · JSON