-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
91 lines (76 loc) · 1.72 KB
/
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
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
import { ApolloServer, gql } from 'apollo-server-express'
import express from 'express'
import { createServer } from 'http'
import cors from 'cors'
import Query from './resolvers/Query/index.js'
import Mutation from './resolvers/Mutation/index.js'
import { createRedisConnection } from './lib/ops/redis.js'
export interface Context {
redis: Awaited<ReturnType<typeof createRedisConnection>>
}
// Construct a schema, using GraphQL schema language
const typeDefs = gql(`
type Query {
advertisers: [String!]!
articles: [Article!]!
}
type Mutation {
createArticle(input: CreateArticleInput!): ArticlePayload
updateArticle(input: UpdateArticleInput!): ArticlePayload
}
input CreateArticleInput {
title: String!
}
input UpdateArticleInput {
id: Int!
title: String
}
type ArticlePayload {
article: Article
}
type Article {
id: Int!
title: String!
}
`);
const resolvers = {
Query,
Mutation,
}
const port = 4000
const app = express()
app.use(cors({ maxAge: 86400 }))
app.use((req, _, next) => {
req.headers['content-type'] = 'application/json'
req.body = {
query: req.body,
}
next()
})
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
const redis = await createRedisConnection()
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [],
debug: true,
context: (): Context => {
return {
redis,
}
},
})
await server.start()
server.applyMiddleware({
app,
path: '/',
bodyParserConfig: { limit: '50mb' },
cors: {
origin: '*',
},
})
createServer(app).listen(port, () => {
console.log(`🌀 HTTP http://localhost:${port}${server.graphqlPath}`)
})
})()