---
eip: 1767
label: EIP-1767
title: GraphQL interface to Ethereum node data
status: Stagnant
type: Standards Track
category: Interface
created: 2019-02-14
official: https://eips.ethereum.org/EIPS/eip-1767
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1767.md
canonical: https://eips.huatuo.xyz/zh/eip-1767/
lang: zh
tldr: 为以太坊节点规定 GraphQL schema 和端点，让客户端用一次查询就能精确获取所需的区块、交易、账户和日志数据。
tags: [graphql, api, node, json-rpc, interface]
---

# EIP-1767: GraphQL interface to Ethereum node data

> This EIP specifies a GraphQL schema for accessing data stored on an Ethereum node. It aims to provide a complete replacement to the read-only information exposed via the present JSON-RPC interface, while improving on usability, consistency, efficiency, and future-proofing.

**TL;DR:** 为以太坊节点规定 GraphQL schema 和端点，让客户端用一次查询就能精确获取所需的区块、交易、账户和日志数据。

## 它解决什么问题？

JSON-RPC 接口有两类弱点。其一，它的规范比较松散，客户端在一些细节上存在分歧，例如如何编码空字节串（`""`、`"0x"` 还是 `"0x0"`）。其二，它返回的是固定的字段组合，因此节点常常要做调用者并不需要的工作：`eth_getBlock` 总是会获取 `totalDifficulty`，而通过 `eth_getTransactionReceipt` 获取区块中的每一张收据，可能意味着一遍又一遍地反序列化相同的数据。

EIP-1767 提议用 GraphQL 作为更干净的读取 API。调用者准确描述自己想要哪些字段，相关数据（区块 → 交易 → 收据 → 日志）在一次往返中返回，而 schema 本身就是正式的规范。

## 它如何运作

- 节点**必须**通过 HTTP 暴露一个 GraphQL 端点，**应当**在 `/graphql` 提供服务，并且**应当**默认使用 8547 端口。它们**可以**在 `/` 提供 GraphiQL 浏览器。
- schema 定义了带有严格十六进制编码的标量（`Bytes32`、`Address`、`Bytes`、`BigInt`、`Long`），以及 `Block`、`Transaction`、`Account`、`Log`、`CallResult`、`SyncState` 和 `Pending` 等类型。
- 顶层查询：`block`、`blocks`、`transaction`、`logs`、`pending`、`gasPrice`、`protocolVersion`、`syncing`。
- 一个 mutation：`sendRawTransaction`。
- 调用和 gas 估算是区块上的字段（`block { call(...) }`），因此它们针对该区块的状态运行。

大多数只读的 JSON-RPC 方法都能直接映射为查询：

| 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...") }` |

节点侧签名（`eth_sign`、`eth_sendTransaction`）、编译器、过滤器和挖矿相关调用被有意排除在外。特定于客户端的扩展必须加前缀，例如 `_geth_`。

## 示例代码

用一次请求获取最新区块中的所有交易及其状态和日志：

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

用 TypeScript 发送：

```ts
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);
```

## 需要注意

- **状态：Stagnant。** EIP 本身没有最终确定，不过规范列出了 go-ethereum（自 1.9.0 起）和 Pantheon（现为 Hyperledger Besu）中的实现。
- 端点通常默认处于关闭状态，需要通过客户端的参数开启。确切的选项和端口请查阅你所用客户端的文档。
- 规范中描述的字段反映的是工作量证明时代的区块（`difficulty`、`ommers`、`mixHash`）。客户端的 schema 此后已经演进；请通过 GraphQL 内省查看实际运行的 schema，而不要依赖这份 EIP。
- JSON-RPC（[EIP-1474](/eip-1474/) 以及当前的执行层 API 规范）仍是主要接口；GraphQL 是可选的补充。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-1767
