-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (99 loc) · 2.85 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { ApolloServer, gql } = require('apollo-server');
const { makeExecutableSchema } = require("graphql-tools");
const PubSub = require('graphql-subscriptions').PubSub;
const pubsub = new PubSub();
const typeDefs = require("./schema/schema");
const { getContract,getWallet,getProvider } = require("./contract/getContract");
let contractInstance = {};
getContract.then(res=>contractInstance=res);
let ethersWalletInstance = getWallet();
async function getTransactionReceipt(transactionHash){
let response =null;
await transactionHash.wait();
response = await getProvider().getTransactionReceipt(transactionHash.hash)
pubsub.publish('transactionMined', {
transactionReceipt: {
transactionHash: response.transactionHash,
blockHash: response.blockHash,
blockNumber:response.blockNumber,
gasUsed:parseInt(response.gasUsed),
status:response.status
}
});
//return response;
}
async function set_Greeter(message) {
let response = null;
let error = null;
await contractInstance.setGreeter(message).then(res => (response = res)).catch(err => (error = err));
// await response.wait();
// response = await getTransactionReceipt(response.hash);
getTransactionReceipt(response);
return response;
}
var flag =0;
// The resolvers
const resolvers = {
Query: {
async greet() {
let message=contractInstance.greet();
return {
address: ethersWalletInstance.address,
message
};
}
},
Mutation: {
async setGreeter(obj, args, context) {
let transactionHash = "";
let blockHash = "";
let blockNumber = 0;
let gasUsed = 0;
let status = false;
const response = set_Greeter(args.message);
await response
.then(res => {
transactionHash = res.hash;
from = res.from;
to = res.to;
})
.catch(err => console.log(err));
return {
transactionHash,
from,
blockNumber,
to
};
},
},
Subscription: {
transactionMined: {
resolve: (payload) => {
console.log(payload);
return payload.transactionReceipt;
},
subscribe: () => pubsub.asyncIterator('transactionMined')
}
// commentAdded: {
// resolve: (payload) => {
// return {
// customData: payload,
// };
// },
// subscribe: () => pubsub.asyncIterator('commentAdded')
// }
}
};
// Put together a schema
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types.
const server = new ApolloServer({ typeDefs, resolvers });
// This `listen` method launches a web-server.
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});