-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphql.js
203 lines (171 loc) · 5.73 KB
/
graphql.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const ethJsUtil = require('ethereumjs-util');
const { withFilter, AuthenticationError } = require('apollo-server');
const { ApolloServer, gql } = require('apollo-server-express');
const { actionableStatus } = require('./helpers/constants');
const { proposalToType, getCurrentActionableStatus } = require('./helpers/utils');
const { getAddressDetails } = require('./dbWrapper/addresses');
const { getDaoInfo } = require('./dbWrapper/dao');
const { pubsub } = require('./pubsub');
const {
getProposal,
getSpecialProposal,
getSpecialProposals,
getProposals,
} = require('./dbWrapper/proposals');
const { proposalStages } = require('./helpers/constants');
const { typeDef: scalarType, resolvers: scalarResolvers } = require('./types/scalar.js');
const { typeDef: userType, resolvers: userResolvers } = require('./types/user.js');
const { typeDef: proposalType, resolvers: proposalResolvers } = require('./types/proposal.js');
const { typeDef: daoType, resolvers: daoResolvers } = require('./types/dao.js');
const queryType = gql`
type Query {
# Find a specific proposal by proposal ID.
fetchProposal(proposalId: String!): Proposal
# Get the current user's information.
fetchCurrentUser: User!
# Get the current user's information.
fetchDao: Dao!
# Find proposals in specific stage
fetchProposals(stage: String!, onlyActionable: Boolean): [Proposal]
}
`;
const mutationType = gql`
type Mutation {
# Sample mutation just to get a pong.
ping: String
}
`;
const subscriptionType = gql`
type Subscription {
# Triggers on any submitted proposal.
proposalSubmitted: Proposal!
# Triggers on any updates of a proposal.
proposalUpdated: Proposal!
# Triggers on any change of the current user.
userUpdated: User!
# Triggers on any change in the daoInfo struct
daoUpdated: Dao!
}
`;
const filterByCurrentAddress = f => (payload, _variables, context, _operation) => (payload
? context.address === f(payload) : false);
const resolvers = {
Query: {
fetchProposal: async (_obj, args, _context, _info) => {
const { proposalId } = args;
let proposal = await getProposal(proposalId);
if (!proposal) {
proposal = await getSpecialProposal(proposalId);
}
return proposal ? proposalToType(proposal) : null;
},
fetchCurrentUser: (_obj, _args, context, _info) => {
if (!context.currentUser) {
throw new Error('Not Authenticated');
}
return context.currentUser;
},
fetchDao: (_obj, _args, _context, _info) => {
return getDaoInfo();
},
fetchProposals: async (_obj, args, context, _info) => {
const { stage, onlyActionable } = args;
const filter = (stage === 'all') ? {} : { stage: stage.toUpperCase() };
const proposals = await getProposals(filter);
const specialProposals = (stage.toUpperCase() === proposalStages.PROPOSAL || stage === 'all') ? await getSpecialProposals() : [];
const allProposals = specialProposals.concat(proposals).map(proposal => ({
...proposal,
actionableStatus: getCurrentActionableStatus(proposal, context.currentUser),
}));
return onlyActionable ? allProposals.filter(proposal => proposal.actionableStatus !== actionableStatus.NONE) : allProposals;
},
},
Mutation: {},
Subscription: {
userUpdated: {
subscribe: withFilter(
(_obj, _args, context, _info) => {
if (!context.currentUser) {
throw new Error('Not Authenticated');
}
return pubsub.asyncIterator('userUpdated');
},
filterByCurrentAddress(payload => payload.userUpdated.address),
),
},
proposalSubmitted: {
subscribe: (_obj, _args, context, _info) => {
if (!context.currentUser) {
throw new Error('Not Authenticated');
}
return pubsub.asyncIterator('proposalSubmitted');
},
},
proposalUpdated: {
subscribe: (_obj, _args, context, _info) => {
if (!context.currentUser) {
throw new Error('Not Authenticated');
}
return pubsub.asyncIterator('proposalUpdated');
},
},
daoUpdated: {
subscribe: () => pubsub.asyncIterator('daoUpdated'),
},
},
};
const signatureAuthorization = (params) => {
const { address, message, signature } = params;
if (address && message && signature) {
const { v, r, s } = ethJsUtil.fromRpcSig(signature);
const prefixedMsg = ethJsUtil.sha3(
Buffer.concat([
Buffer.from('\x19Ethereum Signed Message:\n'),
Buffer.from(String(message.length)),
Buffer.from(message),
]),
);
const publicKey = ethJsUtil.ecrecover(prefixedMsg, v, r, s);
const bufferedAddress = ethJsUtil.pubToAddress(publicKey);
const recoveredAddress = ethJsUtil.bufferToHex(bufferedAddress);
const normalizedAddress = address.toLowerCase();
if (recoveredAddress === normalizedAddress) {
return getAddressDetails(normalizedAddress)
.then(userInfo => ({
address: normalizedAddress,
currentUser: userInfo,
}));
}
throw new AuthenticationError('Invalid address or signature');
} else {
return {};
}
};
module.exports = new ApolloServer({
typeDefs: [
scalarType,
userType,
proposalType,
daoType,
queryType,
mutationType,
subscriptionType,
],
resolvers: {
...scalarResolvers,
...userResolvers,
...proposalResolvers,
...daoResolvers,
...resolvers,
},
context: ({ req, connection }) => {
if (connection) {
return connection.context;
}
return signatureAuthorization(req.headers);
},
subscriptions: {
path: '/websocket',
onConnect: (connectionParams, _webSocket) => signatureAuthorization(connectionParams || {}),
},
});