-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphQL.js
210 lines (180 loc) · 4.97 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
204
205
206
207
208
209
210
const bcrypt = require('bcrypt');
const { graphql } = require('graphql');
const { makeExecutableSchema } = require('graphql-tools');
const db = require('./db');
let getUserById = (userid) => db.one(`
SELECT * FROM users
WHERE userid = ${userid};
`)
let getAllPlantData = (userid) => db.query(`
SELECT * FROM plant_data
WHERE userid = ${userid};
`)
let getPlantDataFor = (userid, hours) => {
let dateAfter = new Date();
dateAfter.setHours( dateAfter.getHours() - hours - 4) //EDT timezone
dateAfter = dateAfter.toISOString();
console.log('dateAFTER', dateAfter)
return db.query(`
SELECT * FROM plant_data
WHERE userid = ${userid}
AND created >= '${dateAfter}';
`)}
let typeDefs = (`
type Query {
currentUser(userid: ID): Data
}
type Data {
user: UserInfo
plantData: [PlantData]
getPlantDataFor(hours: Float): [PlantData]
}
type UserInfo {
username: String
avatar: String
userid: ID
email: String
passw: String
}
type PlantData {
userid: ID
temp: Float
sun: Float
moist: Float
humidity: Float
created: String
dataid: Int
}
type Mutation {
createUser(input: AddUser): UserInfo
addPlantData(input: AddData): PlantData
}
input AddUser {
username: String!
avatar: String
email: String!
passw: String!
}
input AddData {
temp: Float
sun: Float
moist: Float
humidity: Float
}
`)
// ###### queries for a reference and testing ##########
// let query = `
// mutation {
// addPlantData(input: {userid: 1 temp: 70 sun: 300 moist: 24 humidity: 3.2 }) {
// userid
// }
// }
// `
// let query = `
// mutation {
// createUser(input: {username: "test3" email: "[email protected]" passw: "3333"}) {
// userid
// username
// }
// }
// `
// let query = `
// query {
// currentUser(userid: 1) {
// user {
// username
// avatar
// }
// getPlantDataFor(hours: 3) {
// temp
// sun
// moist
// humidity
// created
// }
// }
// }`
let resolvers = {
Query: {
currentUser: (parent, args, userid) => {
return userid
}
},
Data: {
user: (userid) => getUserById(userid),
plantData: (userid) => getAllPlantData(userid),
getPlantDataFor: (userid, args) => getPlantDataFor(userid, args.hours),
},
UserInfo: {
username: (p) => p.username,
avatar: (p) => p.avatar,
userid: (p) => p.userid,
email: (p) => p.email,
},
PlantData: {
userid: (p) => p.userid,
temp: (p) => p.temp,
sun: (p) => p.sun,
moist: (p) => p.moist,
humidity: (p) => p.humidity,
created: (p) => p.created.toISOString(),
dataid: (p) => p.dataid,
},
Mutation: {
createUser: async (parent, args) => {
let {username, email, avatar, passw} = args.input
let hash = await bcrypt.hash(passw, 10);
// no avatar ==> insert default
avatar ? {} : avatar = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQzlDPRr1xSW0lukY2EmVpAx5Ye1S8H5luUVOK2IqFdcsjCDQxK';
let insert = await db.query(`
INSERT INTO users (
username, email, passw, avatar
)
VALUES (
'${username}',
'${email}',
'${hash}',
'${avatar}'
)
RETURNING *;
`)
return insert[0];
},
addPlantData: async (parent, args, userid) => {
let { temp, sun, moist, humidity} = args.input;
//no data ==> insert 0
temp ? {} : temp = 0;
sun ? {} : sun = 0;
moist ? {} : moist = 0;
humidity ? {} : humidity = 0;
let insert = await db.query(`
INSERT INTO plant_data (
userid, temp, sun, moist, humidity
)
VALUES (
'${userid}',
'${temp}',
'${sun}',
'${moist}',
'${humidity}'
)
RETURNING *;
`)
return insert[0];
}
}
}
let schema = makeExecutableSchema({typeDefs, resolvers})
let getResults = async (query, userid) => {
let results = await graphql({
schema,
source: query,
rootValue: resolvers,
contextValue: userid}
).then( res => JSON.stringify(res));
return results
}
// getResults(query).then(console.log)
module.exports = {
getResults
}