-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (125 loc) · 4.88 KB
/
index.js
File metadata and controls
136 lines (125 loc) · 4.88 KB
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
const path = require('path')
const express = require('express')
const app = express()
const cors = require('cors')
const { ApolloServer, UserInputError } = require('apollo-server-express')
const mongoose = require('mongoose')
const typeDefs = require('./typeDefs')
const Guide = require('./schemas/guideSchema')
const Changelog = require('./schemas/changelogSchema')
const config = require('./utils/config')
const Stats = require('./stats')
const statsData = fetchStats()
app.use(cors())
async function fetchStats(difficulty) {
return await Stats(difficulty)
}
// async function fetchStats(difficulty) {
// const url = 'http://localhost:4002/stats'
// const data = await axios.get(url)
// console.log(typeof data)
// return data.data
// }
mongoose
.connect(config.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
.then(() => {
console.log('connected to MongoDB')
})
.catch((error) => {
console.log('error connection to MongoDB:', error.message)
})
const resolvers = {
Query: {
allGuides: async (root, args) => {
const guides = await Guide.find({}).sort({ _id: -1 })
if (!args.first) return guides
else if (!args.after) return guides.slice(0, args.first)
else return guides.slice(args.after, args.after+args.first)
},
allHeroGuides: (root, args) =>
Guide.find({ hero: args.hero }).sort({ _id: -1 }),
guideSearch: (root, args) =>
Guide.find({ title: new RegExp(args.hero, 'i') }),
guideCount: (root, args) => {
if (args.hero) return Guide.collection.countDocuments( {hero: args.hero})
else return Guide.collection.countDocuments()
},
allMatchData: () => {
return statsData
},
victoriousMatches: async (root, args) => {
const vicStats = await statsData
// console.log(vicStats)
const vicGames = vicStats.map(difficulty => difficulty.victoriousGames)
// console.log(statsData)
if (!args.hero) {
if (!args.first && !args.after) return vicGames[args.difficulty]
else if (!args.after) return vicGames[args.difficulty].slice(0, args.first)
else return vicGames[args.difficulty].slice(args.after, args.after+args.first)
}
},
victoriousMatchesCount: async (root, args) => {
const vicStats = await statsData
const vicGames = vicStats.map(difficulty => difficulty.victoriousGames)
if (!args.hero) return vicGames[args.difficulty].length
},
homePageData: async () => {
const vicStats = await statsData
const vicGames = vicStats.map(difficulty => difficulty.victoriousGames).map(difficulty => difficulty.slice(0, 3))
const guides = await Guide.find({}).sort({ _id: -1 })
const changelogs = await Changelog.find({}).sort({ _id: -1})
return {
victoriousMatches: vicGames,
guides: guides.slice(0, 3),
changelogs: changelogs
}
},
individualGame: async (root, args) => {
const individualGameData = await Stats(args.difficulty, args.matchId)
if (individualGameData === 'No match found') {
throw new UserInputError('No match found', {
invalidArgs: args.matchId,
})
}
return individualGameData
},
allChangelogs: (root, args) => Changelog.find({}).sort({ _id: -1}),
heroStats: async (root, args) => {
const heroStatsData = await statsData
const heroStats = heroStatsData.map(difficulty => {
return {
shardWinrates: [...difficulty.shardWinrates].filter(shard => shard.hero === args.hero),
victoriousGames: [...difficulty.victoriousGames].filter(game => game.players.filter(player => player.hero === args.hero).length > 0),
singleHeroStats: difficulty.convertedHeroes[args.hero]
}
})
return heroStats
},
},
Mutation: {
addGuide: (root, args) => {
const newGuide = new Guide({ ...args })
return newGuide.save()
},
addChangelog: (root, args) => {
const newChangelog = new Changelog({ ...args })
return newChangelog.save()
}
}
}
app.use(express.static('build'))
app.get('*', (req, res) => {
let url = path.join(__dirname, './build', 'index.html')
res.sendFile(url)
})
const server = new ApolloServer({
typeDefs,
resolvers,
})
server.applyMiddleware({
path: '/graphql',
app,
})
app.listen({ port: process.env.PORT || 4000 }, () => {
console.log('🚀 Server ready at http://localhost:4000')
})