-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (55 loc) · 1.54 KB
/
index.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
const { ApolloServer, gql } = require('apollo-server');
const alphabet = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
];
let words = [];
let current_word = { text: null };
let last_word_selection = new Date();
const fetchWords = async () => {
for (const letter of alphabet) {
const res = await fetch("https://api.datamuse.com/words?sp=" + letter.toLowerCase() + "????&max=1000");
for(const res_json of await res.json()) {
words = words.concat(res_json.word.toUpperCase().toString());
}
}
};
const getCurrentWord = () => {
let current_time = new Date();
let diff = current_time.getTime() - last_word_selection.getTime();
let hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
let minutes = Math.floor(diff / 1000 / 60);
if(current_word.text === null || minutes >= 1) {
current_word.text = words[Math.floor(Math.random() * words.length)];
console.log(hours, minutes);
last_word_selection = current_time;
}
return current_word;
};
const typeDefs = gql`
type Word {
text: String!
}
type Query {
getWord: Word!
getWords: [String!]!
}
`;
const resolvers = {
Query: {
getWord: () => getCurrentWord(),
getWords: () => words
}
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
fetchWords().then(r => {
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`)
})
}).catch(reason => {
console.log("Unable to fetch words:", reason);
});