使用The Graph 索引链上RDF数据
The GraphQL API是Relation基于The Graph开发的链上图数据查询服务
Last updated
The GraphQL API是Relation基于The Graph开发的链上图数据查询服务
Last updated
curl --location --request POST 'https://api.thegraph.com/subgraphs/name/relationlabs/semantic-sbt' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"{\n turtles(first: 2) {\n id\n owner\n tokenId\n turtle\n }\n}"}'{"data":{"turtles":[{"id":"33456276423","owner":"0x16ff7821a8d293cd2ea07b650fc69ea9206d0615","tokenId":"1","turtle":":Activity1 p:name \"Activity_2049_1\" . :Activity1 p:level 1 . "},{"id":"33456276425","owner":"0x16ff7821a8d293cd2ea07b650fc69ea9206d0615","tokenId":"2","turtle":":Activity2 p:name \"Activity_2049_2\" . :Activity2 p:level 2 . "}]}}npm install @apollo/client graphqlimport { ApolloClient, InMemoryCache, gql } from '@apollo/client'
const APIURL = 'https://api.thegraph.com/subgraphs/name/relationlabs/semantic-sbt'
const tokensQuery = `
query($first: Int, $contract: String) {
turtles(
first: $first, contract: $contract
) {
id
tokenId
owner
turtle
}
}
const client = new ApolloClient({
uri: APIURL,
cache: new InMemoryCache(),
})
client
.query({
query: gql(tokensQuery),
variables: {
first: 2,
contract: '0xb8eDcD887DF79278E227dAb986cb2a91C2347E02',
},
})
.then((data) => console.log('Subgraph data: ', data))
.catch((err) => {
console.log('Error fetching data: ', err)
})update=INSERT DATA
{ <https://test.com/s> <https://test.com/p> <https://test.com/o> . }curl -X POST --data-binary 'update=INSERT DATA { <https://test.com/s> <https://test.com/p> <https://test.com/o> . }' https://your-neptune-endpoint:port/sparqlcurl -X POST --data-binary 'query=select ?s ?p ?o where {?s ?p ?o} limit 10' https://your-neptune-endpoint:port/sparqlimport json
import requests
// 通过The graph的HTTP接口,获取链上对应token的RDF数据,并解析对应的turtle数据对象
def graphql_query():
turtles = []
graphql_endpoint = 'https://api.thegraph.com/subgraphs/name/relationlabs/semantic-sbt'
data = {
'query': '{turtles(where: {contract: "0xb8eDcD887DF79278E227dAb986cb2a91C2347E02", blockNumber_gt: '
'"35661923", blockNumber_lt: "35671923"}) {id owner tokenId turtle}} '
}
res = requests.post(graphql_endpoint, headers={'Content-Type': 'application/json'}, data=json.dumps(data))
print(len(json.loads(res.text)['data']['turtles']))
for item in json.loads(res.text)['data']['turtles']:
turtles.append(item['turtle'])
return turtles
// 将turtle数据对象和对应Schema拼装为sparql格式语句,通过Neptune服务接口,保存至图数据库中
def load_rdf(turtles):
neptune_endpoint = 'https://your-neptune-endpoint:port/sparql'
for turtle in turtles:
sparql = 'update=PREFIX : <http://relationlabs.ai/entity/> ' \
'PREFIX p: <http://relationlabs.ai/property/> ' \
'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ' \
'PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> ' \
'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> ' \
'INSERT DATA {GRAPH <http://relationlabs.ai/relationship> { ' + turtle + ' }}'
res = requests.post(neptune_endpoint,
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'},
data=sparql)
print(res.text)
if __name__ == '__main__':
load_rdf(graphql_query())