Skip to content

Commit 5891294

Browse files
committed
Add typescript
1 parent 0572b63 commit 5891294

23 files changed

+402
-189
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ bazel-*
3232

3333
# JavaScript
3434
node_modules
35+
36+
# TypeScript
37+
dist

package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44
"license": "Apache-2.0",
55
"author": "Alex Kern <[email protected]>",
66
"scripts": {
7-
"start": "nodemon -e js,graphql -x node -r dotenv/config src/index.js",
8-
"debug": "nodemon -e js,graphql -x node --inspect -r dotenv/config src/index.js",
7+
"start": "nodemon -e ts,graphql -x ts-node src/index.ts",
8+
"debug": "nodemon -e ts,graphql -x ts-node --inspect src/index.ts",
99
"playground": "graphql playground",
10-
"dev": "npm-run-all --parallel start playground"
10+
"dev": "npm-run-all --parallel start playground",
11+
"lint": "tslint -c tslint.json 'src/**/*.ts'"
1112
},
1213
"dependencies": {
1314
"dataloader": "^1.4.0",
1415
"graphql-yoga": "1.14.6",
1516
"web3": "^1.0.0-beta.34"
1617
},
1718
"devDependencies": {
19+
"@types/core-js": "^2.5.0",
20+
"@types/es6-promise": "^3.3.0",
21+
"core-js": "^2.5.7",
1822
"dotenv": "5.0.1",
1923
"graphql-cli": "2.16.0",
2024
"nodemon": "1.17.5",
21-
"npm-run-all": "4.1.3"
25+
"npm-run-all": "4.1.3",
26+
"ts-node": "^6.1.0",
27+
"tslint": "^5.10.0",
28+
"typescript": "^2.9.1",
29+
"web3-typescript-typings": "^0.10.2"
2230
}
2331
}

src/context.js

-10
This file was deleted.

src/context.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createWeb3Loaders } from "./web3";
2+
3+
export interface Context {
4+
req: any;
5+
loaders: {
6+
web3: any,
7+
};
8+
}
9+
10+
export function createContext(req) {
11+
return {
12+
...req,
13+
loaders: { web3: createWeb3Loaders() },
14+
};
15+
}

src/index.js

-16
This file was deleted.

src/index.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GraphQLServer } from "graphql-yoga";
2+
import { createContext } from "./context";
3+
import * as resolvers from "./resolvers";
4+
5+
const server = new GraphQLServer({
6+
context: createContext,
7+
resolvers,
8+
typeDefs: "./src/schema.graphql",
9+
});
10+
11+
server.start((info) =>
12+
// tslint:disable-next-line:no-console
13+
console.log(
14+
`Server is running on localhost:${info.port}`,
15+
),
16+
);

src/resolvers/EthereumAddress.js

-12
This file was deleted.

src/resolvers/EthereumAddress.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Context } from "../context";
2+
import { web3, Web3Address } from "../web3";
3+
4+
export const EthereumAddress = {
5+
balance: (parent: Web3Address, args, ctx: Context) => {
6+
return ctx.loaders.web3.balance.load(parent);
7+
},
8+
transactionCount: (parent: Web3Address, args, ctx: Context) => {
9+
return ctx.loaders.web3.transactionCount.load(parent);
10+
},
11+
};

src/resolvers/EthereumBlock.js

-25
This file was deleted.

src/resolvers/EthereumBlock.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Context } from "../context";
2+
import { Web3Block } from "../web3";
3+
4+
export const EthereumBlock = {
5+
parent(parent: Web3Block, args, ctx: Context) {
6+
return ctx.loaders.web3.block.load({
7+
hash: parent.parentHash,
8+
network: parent.network,
9+
});
10+
},
11+
miner(parent: Web3Block, args, ctx: Context) {
12+
return { hash: parent.miner, network: parent.network };
13+
},
14+
transactions(parent: Web3Block, args, ctx: Context) {
15+
return ctx.loaders.web3.transaction.loadMany(
16+
parent.transactions.map((hash) => ({ hash, network: parent.network })),
17+
);
18+
},
19+
transactionCount(parent: Web3Block, args, ctx: Context) {
20+
return ctx.loaders.web3.blockTransactionCount.load({
21+
hash: parent.parentHash,
22+
network: parent.network,
23+
});
24+
},
25+
uncles(parent: Web3Block, args, ctx: Context) {
26+
return ctx.loaders.web3.block.loadMany(
27+
parent.uncles.map((hash) => ({
28+
hash,
29+
network: parent.network,
30+
})),
31+
);
32+
},
33+
};

src/resolvers/EthereumTransaction.js

-15
This file was deleted.

src/resolvers/EthereumTransaction.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Context } from "../context";
2+
import { Web3Transaction } from "../web3";
3+
4+
export const EthereumTransaction = {
5+
block: (parent: Web3Transaction, args, ctx: Context, info) => {
6+
return ctx.loaders.web3.block.load({
7+
hash: parent.blockHash,
8+
network: parent.network,
9+
});
10+
},
11+
from: (parent: Web3Transaction, args, ctx: Context, info) => {
12+
return { network: parent.network, hash: parent.from };
13+
},
14+
to: (parent: Web3Transaction, args, ctx: Context, info) => {
15+
return { network: parent.network, hash: parent.to };
16+
},
17+
};

src/resolvers/Mutation.js

-10
This file was deleted.

src/resolvers/Mutation.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { web3 } from "../web3";
2+
3+
export const Mutation = {
4+
async sendRawTransaction(parent, args, ctx) {
5+
const hash = await web3[args.network].eth.sendRawTransaction(args);
6+
return { hash, network: args.network };
7+
},
8+
};

src/resolvers/Query.js

-25
This file was deleted.

src/resolvers/Query.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Context } from "../context";
2+
import { web3 } from "../web3";
3+
4+
export const Query = {
5+
ethereumGasPrice(parent, { network = "MAINNET" }) {
6+
return web3[network].eth.getGasPrice();
7+
},
8+
9+
ethereumBlockNumber(parent, { network = "MAINNET" }) {
10+
return web3[network].eth.getBlockNumber();
11+
},
12+
13+
ethereumAddress(parent, { hash, network = "MAINNET" }) {
14+
return { hash, network };
15+
},
16+
17+
ethereumBlock(parent, args, ctx: Context) {
18+
return ctx.loaders.web3.block.load(args);
19+
},
20+
21+
ethereumTransaction(parent, args, ctx: Context) {
22+
return ctx.loaders.web3.transaction.load(args);
23+
},
24+
};

src/resolvers/index.js

-13
This file was deleted.

src/resolvers/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { EthereumAddress } from "./EthereumAddress";
2+
export { EthereumBlock } from "./EthereumBlock";
3+
export { EthereumTransaction } from "./EthereumTransaction";
4+
export { Query } from "./Query";
5+
export { Mutation } from "./Mutation";

src/web3.js

-50
This file was deleted.

0 commit comments

Comments
 (0)