Developer Hub
Relation ProtocolRelation ONE APIRelation Graph API
English
English
  • Overview
  • QUICK START
    • Deploy a Semantic SBT contract leveraging Relation Protocol
    • Deploying query services using Graph Indexer
  • KEY CONCEPTS
    • RDF
    • Semantic SBTs
    • Social Graph
  • Architecture
  • Schema Standard
    • Overview
    • How schemas limit smart contracts
    • Store schema
    • List of schemas
  • Contract Open Standard
    • Overview
    • Identity
      • Name Service
    • Relationship
      • Follow
      • Dao
    • Publication
      • Content
      • Privacy Content
  • Open Standard API
    • Introduction
    • EIP-6239
    • Business Interface
      • Identity
      • Relationship
      • Publication
  • Graph Indexer
    • Definition and usage
    • Listen to events
    • To parse RDF data
    • To build a social graph
  • Integrations
    • Quick start
    • Construct a social graph with Relation Protocol
    • List of resources
    • NameService contract guide
    • Follow contract guide
    • Dao contract guide
    • Content contract guide
    • PrivacyContent contract guide
  • Relation Name Service
    • Name Service Api
  • Use Case
  • APPENDIX
    • Deploy a contract using Hardhat
    • SemanticSBT Deployment tool
Powered by GitBook
On this page
  • Dependencies
  • Deploy neo4j
  • A simple Graph Indexer
  1. Integrations

Construct a social graph with Relation Protocol

Last updated 2 years ago

We will demonstrate how to quickly build a graph-indexer to listen to on-chain data, store RDF data using neo4j graph database, and query and show a graph.

Dependencies

Before we start, you should have installed on your computer.

Deploy neo4j

Please refer to for neo4j deployment.

A simple Graph Indexer

We will use nodejs & neo4j-driver to proceed.

This example will listen to the RDF data in between blocks 8701627 and 8702627 on the Goerli network.

  1. Create a new project and install the neo4j-driver and ethers(v5) libraries.

mkdir graph-indexer
cd graph-indexer
npm init -y
npm install neo4j-driver ethers@5
  1. Create an app.js to listen to data on the blockchain:

const ethers = require('ethers')
const rpc = 'https://rpc.ankr.com/eth_goerli'
let abi = ['event CreateRDF(uint256 indexed tokenId, string rdfStatements)']
const provider = new ethers.providers.JsonRpcProvider(rpc)
const iface = new ethers.utils.Interface(abi)
const filter = {}

// connect neo4j-server
const neo4j = require('neo4j-driver')
const uri = 'bolt://localhost:7687'
// neo4j's default user/password are: neo4j/neo4j
const user = 'neo4j'
const password = 'neo4j'
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
const session = driver.session()

// Insert rdf into neo4j
async function insertRdfToNeo4j(rdfString) {
    const session = driver.session()
    try {
        const result = await session.run(
            `CALL n10s.rdf.import.inline("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#>

    ${rdfString}","Turtle");`
        )
        return result
    } catch (error) {}
    session.close()
}

// Listen to new events on the blockchain
provider.on(filter, async (e) => {
  try {
    let event = iface.parseLog(e)
    if (event) {
      const rdf = event.args[1]
      //  Parse
      //  todo: Execute your own business logic. You can filter the RDF data you need.
      //  Insert into the database.
      const res = await insertRdfToNeo4j(rdf)
    }
  } catch (error) {}
})

// You can also specify a filter for data.
const logFilter = {
  fromBlock: 8701627,
  toBlock: 8702627,
  topics: [
        utils.id("CreateRDF(uint256,string)"),
    ]
}
provider
  .getLogs(logFilter)
  .then(function (logs) {
    for (let index = 0; index < logs.length; index++) {
      try {
        const log = logs[index]
        const event = iface.parseLog(log)
        if (event) {
          const rdf = event.args[1]
          insertRdfToNeo4j(rdf)
        }
      } catch (error) {}
    }
  })
  .catch(function (err) {
    console.log(err)
  })
  1. Run the Graph Indexer

node app.js

To query all the "follow" relationship, you can use:

MATCH (node1)-[rel:p__following]->(node2)
RETURN node1,node2

You will see the following graph:

Once the data is inserted, open in your browser, and input match (n) return n in the interface to query all the data.

Node.js
this document
http://localhost:7474/browser/
Figure 8-1 Social Graph