-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
98 lines (88 loc) · 2.5 KB
/
index.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
92
93
94
95
96
97
98
import { getUserFromToken } from '@/utils/auth-utils'
import { ApolloServer } from '@apollo/server'
import { expressMiddleware } from '@apollo/server/express4'
import {
ApolloServerPluginLandingPageLocalDefault,
ApolloServerPluginLandingPageProductionDefault,
} from '@apollo/server/plugin/landingPage/default'
import cors from 'cors'
import express from 'express'
import { readFileSync } from 'fs'
import type { JwtPayload } from 'jsonwebtoken'
import NodeCache from 'node-cache'
import path from 'path'
import { resolvers } from './src/resolvers'
const schemaFiles = [
'enums.gql',
'scalars.gql',
'types.gql',
'inputs.gql',
'schema.gql',
]
const schemaDir = path.join(__dirname, 'src', 'schema')
const typeDefs = schemaFiles.map((file) => {
const filePath = path.join(schemaDir, file)
try {
return readFileSync(filePath, { encoding: 'utf-8' })
} catch (error) {
console.error(`Error reading file ${filePath}:`, error)
throw error
}
})
const port = process.env.PORT || 4000
const app = express()
app.use(
cors({
origin: [
'https://studio.apollographql.com',
'http://localhost:3000',
'https://dashboard.neuland.app',
'https://dev.neuland.app',
'https://neuland.app',
],
})
)
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
plugins: [
Bun.env.NODE_ENV === 'production'
? ApolloServerPluginLandingPageProductionDefault({
footer: false,
})
: ApolloServerPluginLandingPageLocalDefault(),
],
introspection: true,
formatError(formattedError, error) {
console.error(error)
return formattedError
},
})
export const cache = new NodeCache({ stdTTL: 60 * 10 }) // 10 minutes default TTL
await apolloServer.start()
app.use(
'/graphql',
cors(),
express.json(),
expressMiddleware(apolloServer, {
context: async ({ req }): Promise<{ jwtPayload?: JwtPayload }> => {
const authHeader = req.headers.authorization
if (authHeader) {
return {
jwtPayload: await getUserFromToken(authHeader),
}
} else {
return {}
}
},
})
)
app.use(
'/',
express.static(path.join(__dirname, 'docs', 'out'), {
extensions: ['html'],
})
)
app.listen(port, () => {
console.log('🚀 Server ready at http://localhost:' + port + '/graphql')
})