-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
70 lines (60 loc) · 1.67 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
67
68
69
70
require('dotenv').config()
const express = require('express')
const connect = require('./db')
const cors = require('cors')
const PORT = process.env.PORT || 8080
const { ApolloServer, gql } = require('apollo-server-express')
const app = express()
const fs = require('fs')
const path = require('path')
const {
addRepository,
getRepositories,
getIssues,
getLabels,
getSpecificRepository
} = require('./controllers')
const schema = fs.readFileSync(path.resolve(__dirname, './models/issues.graphql'), 'utf-8')
const typeDefs = gql(schema)
const resolvers = {
Query: {
repositories: async () => {
const data = await getRepositories()
return data
},
issues: async (_, { pathname }) => {
const data = await getIssues(pathname)
return data.issues
},
label: async (_, { url }) => {
const data = await getLabels(url)
return data.issues
},
repository: async (_, { pathname }) => {
const data = await getSpecificRepository(pathname)
return data
}
}
}
const server = new ApolloServer({ typeDefs, resolvers })
server.applyMiddleware({ app })
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.post('/repository', async (req, res) => {
const { url } = req.body
if (!url) {
res.status(400).json({ message: 'http://http.cat/400' })
}
addRepository(url)
.then((issues) => {
res.json({ message: 'Ok: 203', status: true })
})
.catch((err) => {
res.status(404).json({ message: 'http://http.cat/404', status: false })
console.log(err)
})
})
connect().then(() => {
app.listen(PORT, '0.0.0.0', () => console.log(`running on ${PORT}`))
})