forked from graphql-hive/graphql-yoga
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
191 lines (166 loc) 路 5.06 KB
/
Copy pathindex.ts
File metadata and controls
191 lines (166 loc) 路 5.06 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as express from 'express'
import * as cors from 'cors'
import * as fs from 'fs'
import { importSchema } from 'graphql-import'
import * as path from 'path'
import expressPlayground from 'graphql-playground-middleware-express'
import { SubscriptionServer } from 'subscriptions-transport-ws'
import { createServer } from 'http'
import { execute, subscribe, GraphQLSchema } from 'graphql'
import { apolloUploadExpress, GraphQLUpload } from 'apollo-upload-server'
import { graphqlExpress } from 'apollo-server-express'
import { makeExecutableSchema } from 'graphql-tools'
export { PubSub, withFilter } from 'graphql-subscriptions'
import { Props, Options } from './types'
export { Options }
export class GraphQLServer {
express: express.Application
subscriptionServer: SubscriptionServer | null
options: Options
executableSchema: GraphQLSchema
protected context: any
constructor(props: Props) {
const defaultOptions: Options = {
disableSubscriptions: false,
tracing: { mode: 'http-header' },
port: process.env.PORT ? parseInt(process.env.PORT, 10) : 4000,
endpoint: '/',
subscriptionsEndpoint: '/',
playgroundEndpoint: '/',
disablePlayground: false,
}
this.options = { ...defaultOptions, ...props.options }
if (!this.options.disableSubscriptions) {
this.options.subscriptionsEndpoint = undefined
}
this.express = express()
// CORS support
if (this.options.cors) {
this.express.use(cors(this.options.cors))
} else if (this.options.cors !== false) {
this.express.use(cors())
}
this.express.post(
this.options.endpoint,
express.json(),
apolloUploadExpress(this.options.uploads),
)
this.subscriptionServer = null
this.context = props.context
if (props.schema) {
this.executableSchema = props.schema
} else if (props.typeDefs && props.resolvers) {
let { typeDefs, resolvers } = props
// read from .graphql file if path provided
if (typeDefs.endsWith('graphql')) {
const schemaPath = path.isAbsolute(typeDefs)
? path.resolve(typeDefs)
: path.resolve(typeDefs)
if (!fs.existsSync(schemaPath)) {
throw new Error(`No schema found for path: ${schemaPath}`)
}
typeDefs = importSchema(schemaPath)
}
const uploadMixin = typeDefs.includes('scalar Upload')
? { Upload: GraphQLUpload }
: {}
this.executableSchema = makeExecutableSchema({
typeDefs,
resolvers: {
...uploadMixin,
...resolvers,
},
})
}
}
start(callback: (() => void) = () => null): Promise<void> {
const app = this.express
const {
port,
endpoint,
disablePlayground,
disableSubscriptions,
playgroundEndpoint,
subscriptionsEndpoint,
} = this.options
const tracing = (req: express.Request) => {
const t = this.options.tracing
if (typeof t === 'boolean') {
return t
} else if (t.mode === 'http-header') {
return req.get('x-apollo-tracing') !== undefined
} else {
return t.mode === 'enabled'
}
}
app.post(
endpoint,
graphqlExpress(async request => {
let context
try {
context =
typeof this.context === 'function'
? await this.context({ request })
: this.context
} catch (e) {
console.error(e)
throw e
}
return {
schema: this.executableSchema,
tracing: tracing(request),
context,
}
}),
)
if (!disablePlayground) {
const isDev =
process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'development'
const playgroundOptions = isDev
? { useGraphQLConfig: true, env: process.env }
: { endpoint, subscriptionsEndpoint }
app.get(playgroundEndpoint, expressPlayground(playgroundOptions))
}
if (!this.executableSchema) {
throw new Error('No schema defined')
}
return new Promise((resolve, reject) => {
if (disableSubscriptions) {
app.listen(port, () => {
callback()
resolve()
})
} else {
const combinedServer = createServer(app)
combinedServer.listen(port, () => {
callback()
resolve()
})
this.subscriptionServer = SubscriptionServer.create(
{
schema: this.executableSchema,
execute,
subscribe,
onOperation: async (message, connection, webSocket) => {
let context
try {
context =
typeof this.context === 'function'
? await this.context({ connection })
: this.context
} catch (e) {
console.error(e)
throw e
}
return { ...connection, context }
},
},
{
server: combinedServer,
path: subscriptionsEndpoint,
},
)
}
})
}
}