子图 > 操作指南
3 分钟
使用The Graph上的子图从Polymarket查询区块链数据
通过The Graph网络上的子图使用GraphQL查询Polymarket的链上数据。子图是由The Graph支持的去中心化API,The Graph是一种用于索引和查询区块链数据的协议。
Graph Explorer上的Polymarket子图
您可以在The Graph Explorer的Polymarket子图页面上看到一个交互式查询场,在那里您可以测试任何查询。

如何使用可视化查询编辑器
可视化查询编辑器可帮助您测试子图中的示例查询。
您可以使用GraphiQL Explorer通过单击所需的字段来编写GraphQL查询。
示例查询:从Polymarket获取前5位最高支出
1{2  redemptions(orderBy: payout, orderDirection: desc, first: 5) {3    payout4    redeemer5    id6    timestamp7  }8}示例输出
1{2  "data": {3    "redemptions": [4      {5        "id": "0x8fbb68b7c0cbe9aca6024d063a843a23d046b5522270fd25c6a81c511cf517d1_0x3b",6        "payout": "6274509531681",7        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",8        "timestamp": "1722929672"9      },10      {11        "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0x7",12        "payout": "2246253575996",13        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",14        "timestamp": "1726701528"15      },16      {17        "id": "0x983b71c64b5075fc1179f4e03849af9c727be60de71c9e86e37ad0b3e43f9db9_0x26",18        "payout": "2135448291991",19        "redeemer": "0x5a181dcf3eb53a09fb32b20a5a9312fb8d26f689",20        "timestamp": "1704932625"21      },22      {23        "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0xa",24        "payout": "1917395333835",25        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",26        "timestamp": "1726701528"27      },28      {29        "id": "0xfe82e117201f5169abc822281ccf0469e6b3740fcb4e799d1b599f83b8f11656_0x30",30        "payout": "1862505580000",31        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",32        "timestamp": "1722929866"33      }34    ]35  }36}Polymarket’s GraphQL模式
此子图的模式在Polymarket的GitHub中定义。
Polymarket子图端点
https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp
Polymarket子图端点在Graph Explorer上可用。

如何获得您自己的API密钥
您可以在Graph Explorer中的任何子图上使用此API键,而且它不仅限于Polymarket。
每月10万次查询是免费的,非常适合您的副项目!
其他Polymarket子图
如何使用API进行查询
您可以将任何GraphQL查询传递给Polymarket端点,并接收json格式的数据。
以下代码示例将返回与上述完全相同的输出。
node.js中的示例代码
1const axios = require('axios');23const graphqlQuery = `{4  positions(first: 5) {5    condition6    outcomeIndex7  }8};910const queryUrl = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp'1112const graphQLRequest = {13  method: 'post',14  url: queryUrl,15  data: {16    query: graphqlQuery,17  },18};1920// Send the GraphQL query21axios(graphQLRequest)22  .then((response) => {23    // Handle the response here24    const data = response.data.data25    console.log(data)2627  })28  .catch((error) => {29    // Handle any errors30    console.error(error);31  });其他资源
有关从您的子图查询数据的更多信息,请阅读此处。
要探索优化和自定义子图以获得更好性能的所有方法,请在此处阅读有关创建子图的更多信息。