subspace/test/test-graphql-2.js

71 lines
1.6 KiB
JavaScript
Raw Normal View History

const { makeExecutableSchema } = require("graphql-tools");
const gql = require( "graphql-tag");
const graphql = require( "reactive-graphql").graphql;
2019-09-27 20:04:01 +00:00
const Subspace = require('../dist/node.js');
const Web3Eth = require('web3-eth');
const {deployEscrowContract} = require('./utils-web3');
let eth = new Web3Eth("ws://localhost:8545");
async function run() {
let accounts = await eth.getAccounts();
var EscrowContract = await deployEscrowContract(eth)
await EscrowContract.methods.createEscrow(1, accounts[0], accounts[1]).send({from: accounts[0]})
await EscrowContract.methods.createEscrow(1, accounts[1], accounts[2]).send({from: accounts[0]})
await EscrowContract.methods.createEscrow(1, accounts[1], accounts[0]).send({from: accounts[0]})
await EscrowContract.methods.createEscrow(1, accounts[0], accounts[2]).send({from: accounts[0]})
2019-09-27 20:04:01 +00:00
const subspace = new Subspace(eth.currentProvider);
2019-09-27 20:04:01 +00:00
await subspace.init()
setInterval(async () => {
await EscrowContract.methods.createEscrow(1, accounts[0], accounts[1]).send({from: accounts[0]})
}, 1 * 1000)
const typeDefs = `
type Escrow {
buyer: String!
seller: String!
}
type Query {
escrows: Escrow!
}
`;
const resolvers = {
Query: {
escrows: () => {
2019-09-27 20:04:01 +00:00
return subspace.trackEvent(EscrowContract, 'Created', { filter: { buyer: accounts[0] }, fromBlock: 1 })
}
}
};
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const query = gql`
query {
escrows {
buyer
}
}
`;
const stream = graphql(schema, query);
// stream is an Observable
stream.subscribe(res => console.log(res));
// .
}
run()