Skip to content

Commit ae72da4

Browse files
authored
Merge pull request #63 from embarklabs/feat/graphql-example
feat: graphql example with react
2 parents f821569 + ddedf64 commit ae72da4

File tree

11 files changed

+11922
-1
lines changed

11 files changed

+11922
-1
lines changed

Diff for: examples/react-graphql-example1/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
subspace - react graphql example
2+
===
3+
Simple application using graphql queries to receive a stream of events
4+
This app will deploy a test contract to **Ganache**.
5+
6+
## Requirements
7+
- `ganache-cli`
8+
- `yarn` or `npm` installed.
9+
10+
## Install
11+
In the parent folder, install, build and link the package with `yarn` or `npm`
12+
```
13+
yarn
14+
yarn build:dev
15+
yarn link
16+
```
17+
Then in the current folder link `@embarklabs/subspace`, and install the packages
18+
```
19+
yarn link "@embarklabs/subspace"
20+
yarn
21+
```
22+
23+
## Usage
24+
In a terminal execute
25+
```
26+
ganache-cli
27+
```
28+
29+
In a different session, execute
30+
31+
```
32+
yarn run deploy
33+
```
34+
35+
And then:
36+
37+
```
38+
yarn run start
39+
```
40+
41+
Browse the DApp in [http://localhost:3000](http://localhost:3000)
42+
43+
44+
*Note*: this is a simple example application that does not include error handling for the web3 connection. Be sure `ganache-cli` is running in `localhost:8545` before browsing the dapp.
45+
46+
47+
### node-gyp problems
48+
node-gyp can cause problems, because it requires a C++ compiler.
49+
50+
If you do have problems caused by it, first follow the installation steps for your OS [here](https://github.com/nodejs/node-gyp#installation).
51+
52+
If you still have problems and are on Windows, try the following:
53+
- run `npm config set msvs_version 2015` before `npm install`
54+
- Repair Windows Build tools that the node-gyp doc made you install. If it tells you to remove a conflicting version do it. After the repair succeeded, reboot.

Diff for: examples/react-graphql-example1/deploy.js

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
const Web3 = require("web3");
2+
const web3 = new Web3("ws://localhost:8545");
3+
const fs = require("fs-extra");
4+
5+
const abi = [
6+
{
7+
"constant": false,
8+
"inputs": [
9+
{
10+
"internalType": "string",
11+
"name": "title",
12+
"type": "string"
13+
}
14+
],
15+
"name": "createProduct",
16+
"outputs": [],
17+
"payable": false,
18+
"stateMutability": "nonpayable",
19+
"type": "function"
20+
},
21+
{
22+
"constant": false,
23+
"inputs": [
24+
{
25+
"internalType": "uint256",
26+
"name": "productId",
27+
"type": "uint256"
28+
},
29+
{
30+
"internalType": "string",
31+
"name": "title",
32+
"type": "string"
33+
}
34+
],
35+
"name": "editProduct",
36+
"outputs": [],
37+
"payable": false,
38+
"stateMutability": "nonpayable",
39+
"type": "function"
40+
},
41+
{
42+
"constant": true,
43+
"inputs": [
44+
{
45+
"internalType": "uint256",
46+
"name": "",
47+
"type": "uint256"
48+
}
49+
],
50+
"name": "products",
51+
"outputs": [
52+
{
53+
"internalType": "string",
54+
"name": "title",
55+
"type": "string"
56+
},
57+
{
58+
"internalType": "uint256",
59+
"name": "latestRating",
60+
"type": "uint256"
61+
}
62+
],
63+
"payable": false,
64+
"stateMutability": "view",
65+
"type": "function"
66+
},
67+
{
68+
"constant": false,
69+
"inputs": [
70+
{
71+
"internalType": "uint256",
72+
"name": "productId",
73+
"type": "uint256"
74+
},
75+
{
76+
"internalType": "uint256",
77+
"name": "rating",
78+
"type": "uint256"
79+
}
80+
],
81+
"name": "rateProduct",
82+
"outputs": [],
83+
"payable": false,
84+
"stateMutability": "nonpayable",
85+
"type": "function"
86+
},
87+
{
88+
"inputs": [],
89+
"payable": false,
90+
"stateMutability": "nonpayable",
91+
"type": "constructor"
92+
},
93+
{
94+
"payable": true,
95+
"stateMutability": "payable",
96+
"type": "fallback"
97+
},
98+
{
99+
"anonymous": false,
100+
"inputs": [
101+
{
102+
"indexed": false,
103+
"internalType": "uint256",
104+
"name": "productId",
105+
"type": "uint256"
106+
}
107+
],
108+
"name": "newProduct",
109+
"type": "event"
110+
},
111+
{
112+
"anonymous": false,
113+
"inputs": [
114+
{
115+
"indexed": true,
116+
"internalType": "uint256",
117+
"name": "productId",
118+
"type": "uint256"
119+
},
120+
{
121+
"indexed": false,
122+
"internalType": "uint256",
123+
"name": "rating",
124+
"type": "uint256"
125+
}
126+
],
127+
"name": "Rating",
128+
"type": "event"
129+
}
130+
];
131+
132+
const data = "0x608060405234801561001057600080fd5b506100556040518060400160405280600681526020017f6c6170746f70000000000000000000000000000000000000000000000000000081525061005a60201b60201c565b610244565b600080805480919060010161006f91906100ee565b9050600080828154811061007f57fe5b90600052602060002090600202019050828160000190805190602001906100a7929190610120565b50600281600101819055507f5dc23a654c93455cd36741580d5c1fafc8f35c429bb831bc768008bd655a1219826040518082815260200191505060405180910390a1505050565b81548183558181111561011b5760020281600202836000526020600020918201910161011a91906101a0565b5b505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061016157805160ff191683800117855561018f565b8280016001018555821561018f579182015b8281111561018e578251825591602001919060010190610173565b5b50905061019c91906101d7565b5090565b6101d491905b808211156101d057600080820160006101bf91906101fc565b6001820160009055506002016101a6565b5090565b90565b6101f991905b808211156101f55760008160009055506001016101dd565b5090565b90565b50805460018160011615610100020316600290046000825580601f106102225750610241565b601f01602090049060005260206000209081019061024091906101d7565b5b50565b610659806102536000396000f3fe60806040526004361061003f5760003560e01c806302ec06be1461004157806311eaa75a146101095780637acc0b20146101db578063f4ef9e3614610296575b005b34801561004d57600080fd5b506101076004803603602081101561006457600080fd5b810190808035906020019064010000000081111561008157600080fd5b82018360208201111561009357600080fd5b803590602001918460018302840111640100000000831117156100b557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506102db565b005b34801561011557600080fd5b506101d96004803603604081101561012c57600080fd5b81019080803590602001909291908035906020019064010000000081111561015357600080fd5b82018360208201111561016557600080fd5b8035906020019184600183028401116401000000008311171561018757600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061036f565b005b3480156101e757600080fd5b50610214600480360360208110156101fe57600080fd5b81019080803590602001909291905050506103a6565b6040518080602001838152602001828103825284818151815260200191508051906020019080838360005b8381101561025a57808201518184015260208101905061023f565b50505050905090810190601f1680156102875780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b3480156102a257600080fd5b506102d9600480360360408110156102b957600080fd5b81019080803590602001909291908035906020019092919050505061046f565b005b60008080548091906001016102f091906104ce565b9050600080828154811061030057fe5b9060005260206000209060020201905082816000019080519060200190610328929190610500565b50600281600101819055507f5dc23a654c93455cd36741580d5c1fafc8f35c429bb831bc768008bd655a1219826040518082815260200191505060405180910390a1505050565b806000838154811061037d57fe5b906000526020600020906002020160000190805190602001906103a1929190610500565b505050565b600081815481106103b357fe5b9060005260206000209060020201600091509050806000018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561045f5780601f106104345761010080835404028352916020019161045f565b820191906000526020600020905b81548152906001019060200180831161044257829003601f168201915b5050505050908060010154905082565b817ffdefdf8d82459f7b1eb157e5c44cbe6ee73d8ddd387511fe3622a3ee663b4697826040518082815260200191505060405180910390a280600083815481106104b557fe5b9060005260206000209060020201600101819055505050565b8154818355818111156104fb576002028160020283600052602060002091820191016104fa9190610580565b5b505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061054157805160ff191683800117855561056f565b8280016001018555821561056f579182015b8281111561056e578251825591602001919060010190610553565b5b50905061057c91906105b7565b5090565b6105b491905b808211156105b0576000808201600061059f91906105dc565b600182016000905550600201610586565b5090565b90565b6105d991905b808211156105d55760008160009055506001016105bd565b5090565b90565b50805460018160011615610100020316600290046000825580601f106106025750610621565b601f01602090049060005260206000209081019061062091906105b7565b5b5056fea265627a7a72315820283a23649d3076ca075a3d9190d7b69b6665e8b1d83e5240c8cfbe34dc9d9b3364736f6c634300050b0032";
133+
const MyContract = new web3.eth.Contract(abi, {data, gas: "800000"});
134+
135+
(async () => {
136+
if (!web3.eth.defaultAccount) {
137+
const accounts = await web3.eth.getAccounts();
138+
web3.eth.defaultAccount = accounts[0];
139+
}
140+
const instance = await MyContract.deploy().send({ from: web3.eth.defaultAccount });
141+
142+
let address = instance.options.address;
143+
144+
console.dir("deployment done!");
145+
146+
fs.writeJsonSync("./src/contract.json", { address, abi });
147+
148+
console.dir("artifact written to src/contract.json")
149+
150+
process.exit(0)
151+
})()

Diff for: examples/react-graphql-example1/package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "observables",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"fs-extra": "^8.1.0",
7+
"graphql-tag": "^2.10.1",
8+
"graphql-tools": "^4.0.6",
9+
"react": "^16.9.0",
10+
"react-dom": "^16.9.0",
11+
"react-scripts": "3.1.1",
12+
"reactive-graphql": "^4.0.1",
13+
"rxjs": "^6.5.2",
14+
"web3": "^1.2.1"
15+
},
16+
"scripts": {
17+
"start": "react-scripts start",
18+
"deploy": "node deploy.js",
19+
"build": "react-scripts build",
20+
"test": "react-scripts test",
21+
"eject": "react-scripts eject"
22+
},
23+
"eslintConfig": {
24+
"extends": "react-app"
25+
},
26+
"browserslist": {
27+
"production": [
28+
">0.2%",
29+
"not dead",
30+
"not op_mini all"
31+
],
32+
"development": [
33+
"last 1 chrome version",
34+
"last 1 firefox version",
35+
"last 1 safari version"
36+
]
37+
}
38+
}

Diff for: examples/react-graphql-example1/public/index.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>React App</title>
6+
</head>
7+
<body>
8+
<noscript>You need to enable JavaScript to run this app.</noscript>
9+
<div id="root"></div>
10+
<!--
11+
This HTML file is a template.
12+
If you open it directly in the browser, you will see an empty page.
13+
14+
You can add webfonts, meta tags, or analytics to this file.
15+
The build step will place the bundled scripts into the <body> tag.
16+
17+
To begin the development, run `npm start` or `yarn start`.
18+
To create a production bundle, use `npm run build` or `yarn build`.
19+
-->
20+
</body>
21+
</html>

Diff for: examples/react-graphql-example1/src/App.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import React from "react";
2+
import Subspace from "@status-im/subspace";
3+
import { $average, $latest } from "@embarklabs/subspace";
4+
import { makeExecutableSchema } from "graphql-tools";
5+
import { graphql } from "reactive-graphql";
6+
import { map, scan } from "rxjs/operators";
7+
import web3 from "./web3";
8+
import { abi, address } from "./contract.json";
9+
10+
let Product;
11+
12+
class App extends React.Component {
13+
state = {
14+
userRating: 0,
15+
averageRating: 0,
16+
last5Ratings: [],
17+
filteredRatings: []
18+
};
19+
20+
async componentDidMount() {
21+
const subspace = new Subspace(web3.currentProvider);
22+
await subspace.init();
23+
24+
Product = subspace.contract({ abi, address });
25+
26+
const typeDefs = `
27+
type Query {
28+
averageRating: Float!
29+
last5Ratings: [Int!]
30+
filteredRatings(gte: Int): [Int!]
31+
}
32+
`;
33+
34+
const rating$ = Product.events.Rating.track()
35+
.map("rating")
36+
.pipe(map(x => parseInt(x)));
37+
38+
const resolvers = {
39+
Query: {
40+
averageRating: () => rating$.pipe($average()),
41+
last5Ratings: () => rating$.pipe($latest(5)),
42+
filteredRatings: (a, conditions) => {
43+
const gte = (conditions && conditions.gte) || 0;
44+
return rating$.pipe(
45+
scan((accum, val) => {
46+
if (val >= gte) {
47+
return [...accum, val];
48+
} else {
49+
return accum;
50+
}
51+
}, [])
52+
);
53+
}
54+
}
55+
};
56+
57+
const schema = makeExecutableSchema({ typeDefs, resolvers });
58+
59+
const summaryQuery = `
60+
query {
61+
averageRating
62+
last5Ratings
63+
}
64+
`;
65+
66+
graphql(schema, summaryQuery).subscribe(
67+
({ data: { averageRating, last5Ratings } }) => {
68+
this.setState({ averageRating, last5Ratings });
69+
}
70+
);
71+
72+
const filterRatingsQuery = `
73+
query {
74+
filteredRatings(gte: 3)
75+
}
76+
`;
77+
78+
graphql(schema, filterRatingsQuery).subscribe(
79+
({ data: { filteredRatings } }) => {
80+
this.setState({ filteredRatings });
81+
}
82+
);
83+
}
84+
85+
rateProduct = async () => {
86+
let accounts = await web3.eth.getAccounts();
87+
await Product.methods
88+
.rateProduct(0, this.state.userRating)
89+
.send({ from: accounts[0] });
90+
};
91+
92+
render() {
93+
return (
94+
<div>
95+
<button onClick={this.rateProduct}>Rate Product</button>
96+
<input
97+
type="number"
98+
value={this.state.userRating}
99+
onChange={evt =>
100+
this.setState({ userRating: parseInt(evt.target.value) })
101+
}
102+
/>
103+
<ul>
104+
<li><b>average rating: </b> {this.state.averageRating}</li>
105+
<li><b>last 5 ratings: </b> {(this.state.last5Ratings || []).join(', ')}</li>
106+
<li><b>all ratings greater than 2:</b> {(this.state.filteredRatings || []).join(', ')}</li>
107+
</ul>
108+
</div>
109+
);
110+
}
111+
}
112+
113+
export default App;

Diff for: examples/react-graphql-example1/src/MyContract.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import web3 from './web3';
2+
import {abi, address} from './contract.json'
3+
4+
const MyContract = new web3.eth.Contract(abi, {from: web3.eth.default, gas: "800000"});
5+
MyContract.options.address = address;
6+
7+
MyContract.getInstance = async () => {
8+
if (!web3.eth.defaultAccount) {
9+
const accounts = await web3.eth.getAccounts();
10+
web3.eth.defaultAccount = accounts[0];
11+
}
12+
MyContract.options.from = web3.eth.defaultAccount;
13+
return MyContract;
14+
}
15+
16+
export default MyContract;

0 commit comments

Comments
 (0)