-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathserver.ts
39 lines (31 loc) · 865 Bytes
/
server.ts
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
import { makeExecutableSchema, ApolloServer } from 'apollo-server-micro'
import { default as typeDefs } from './schema'
import resolvers from './resolvers'
import fetch from 'node-fetch'
import microCors = require('micro-cors');
const schema = makeExecutableSchema({
typeDefs,
resolvers
})
let results = null
const server = new ApolloServer({
schema,
playground: true,
introspection: true,
context() {
const getResults = async () => {
if (results) {
return results
}
const res = await fetch('https://pomber.github.io/covid19/timeseries.json')
results = await res.json()
return results
}
return {
getResults
}
},
})
const cors = microCors()
const handler = server.createHandler({ path: '/' })
export default cors((req, res) => req.method === 'OPTIONS' ? res.end() : handler(req, res))