-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js.bkp
44 lines (39 loc) · 1.14 KB
/
resolvers.js.bkp
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
const accounts = [
{ name: 'Beknar' },
{ name: 'Roman' },
];
const transactions = [
{ id: 0, from: 'Roman', to: 'Beknar', amount: 1000 },
];
module.exports = {
Query: {
accounts: () => accounts,
transactions: () => transactions,
},
Mutation: {
addAccount: (parent, { name }) => {
const newAccount = { id: accounts.length, name };
accounts.push(newAccount);
return account;
},
sendMoney: (parent, { from, to, amount }, { pubsub }) => {
const newTransaction = { id: transactions.length, from, to, amount };
transactions.push(newTransaction);
pubsub.publish(to, { newTransaction: newTransaction });
return newTransaction;
},
},
Subscription: {
newTransaction: {
subscribe: (parent, { name }, { pubsub }) => pubsub.asyncIterator(name),
}
},
Account: {
sent: (parent) => transactions.filter(tr => parent.name === tr.from),
received: (parent) => transactions.filter(tr => parent.name === tr.name),
},
Transaction: {
from: (parent) => accounts.find(acc => acc.name === parent.from),
to: (parent) => accounts.find(acc => acc.name === parent.to),
}
}