-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLHandler+HTTP.swift
More file actions
269 lines (248 loc) · 9.9 KB
/
Copy pathGraphQLHandler+HTTP.swift
File metadata and controls
269 lines (248 loc) · 9.9 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import Foundation
import GraphQL
import HTTPTypes
import Hummingbird
import NIOCore
extension GraphQLHandler {
/// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#get
func handleGet(request: Request, context: Context, jsonDecoder: JSONDecoder) async throws
-> Response
{
guard config.allowGet else {
throw HTTPError(.methodNotAllowed, message: "GET requests are disallowed")
}
// Decode query parameters as GraphQLRequest
let queryParameters = request.uri.queryParameters
guard let query = queryParameters.get("query") else {
throw HTTPError(.badRequest, message: "`query` parameter is required")
}
let variables: [String: Map]
if let queryVariables = queryParameters.get("variables") {
do {
variables = try jsonDecoder.decode(
[String: Map].self,
from: Data(queryVariables.utf8)
)
} catch {
throw HTTPError(
.badRequest,
message: "`variable` parameter could not be decoded: \(error)"
)
}
} else {
variables = [:]
}
let graphQLRequest = GraphQLRequest(
query: query,
operationName: queryParameters.get("operationName"),
variables: variables
)
let documentAST: Document
let operationType: OperationType
do {
documentAST = try parse(
source: Source(body: graphQLRequest.query, name: graphQLRequest.operationName ?? "")
)
operationType = try Self.operationTypeOf(
document: documentAST,
named: graphQLRequest.operationName
)
} catch {
// Indicates a request parsing error
throw HTTPError(.badRequest, message: error.localizedDescription)
}
guard operationType != .mutation else {
throw HTTPError(.methodNotAllowed, message: "Mutations using GET are disallowed")
}
let graphQLContextComputationInputs = GraphQLContextComputationInputs<
Context, WebSocketInitResult
>(
hummingbirdRequest: request,
hummingbirdContext: context,
graphQLRequest: graphQLRequest,
websocketInitResult: nil
)
let graphQLContext = try await computeContext(graphQLContextComputationInputs)
let result = await execute(
documentAST: documentAST,
operationName: graphQLRequest.operationName,
variables: graphQLRequest.variables,
context: graphQLContext,
additionalValidationRules: config.additionalValidationRules
)
return try encodeResponse(result: result, request: request, context: context)
}
/// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#post
func handlePost(request: Request, context: Context) async throws -> Response {
guard let contentType = request.headers[.contentType] else {
throw HTTPError(.unsupportedMediaType, message: "Missing `Content-Type` header")
}
guard let mediaType = MediaType(from: contentType) else { throw HTTPError(.badRequest) }
let graphQLRequest: GraphQLRequest
switch mediaType {
case .applicationJson, .applicationJsonGraphQL:
do {
graphQLRequest = try await config.coders.jsonDecoder.decode(
GraphQLRequest.self,
from: request,
context: context
)
} catch {
throw HTTPError(.badRequest, message: error.localizedDescription)
}
case .applicationUrlEncoded:
do {
graphQLRequest = try await config.coders.urlEncodedFormDecoder.decode(
GraphQLRequest.self,
from: request,
context: context
)
} catch {
throw HTTPError(.badRequest, message: error.localizedDescription)
}
default:
throw HTTPError(.unsupportedMediaType)
}
let documentAST: Document
do {
documentAST = try parse(
source: Source(body: graphQLRequest.query, name: graphQLRequest.operationName ?? "")
)
} catch let error as GraphQLError {
// Indicates a request parsing error
let result = GraphQLResult(data: nil, errors: [error])
return try encodeResponse(result: result, request: request, context: context)
}
let graphQLContextComputationInputs = GraphQLContextComputationInputs<
Context, WebSocketInitResult
>(
hummingbirdRequest: request,
hummingbirdContext: context,
graphQLRequest: graphQLRequest,
websocketInitResult: nil
)
let graphQLContext = try await computeContext(graphQLContextComputationInputs)
let result = await execute(
documentAST: documentAST,
operationName: graphQLRequest.operationName,
variables: graphQLRequest.variables,
context: graphQLContext,
additionalValidationRules: config.additionalValidationRules
)
return try encodeResponse(result: result, request: request, context: context)
}
private func execute(
documentAST: Document,
operationName: String?,
variables: [String: Map],
context: GraphQLContext,
additionalValidationRules: [@Sendable (ValidationContext) -> Visitor]
) async -> GraphQLResult {
// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#validation
// Validate schema
let schemaValidationErrors = validateSchema(schema: schema)
guard schemaValidationErrors.isEmpty else {
return GraphQLResult(errors: schemaValidationErrors)
}
// Validate request
let validationRules = GraphQL.specifiedRules + additionalValidationRules
let validationErrors = validate(
schema: schema,
ast: documentAST,
rules: validationRules
)
guard validationErrors.isEmpty else {
return GraphQLResult(errors: validationErrors)
}
// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#execution
let result: GraphQLResult
do {
result = try await GraphQL.execute(
schema: schema,
documentAST: documentAST,
rootValue: rootValue,
context: context,
variableValues: variables,
operationName: operationName
)
} catch let error as GraphQLError {
// This indicates a request parsing error
return GraphQLResult(data: nil, errors: [error])
} catch {
return GraphQLResult(
data: nil,
errors: [GraphQLError(message: error.localizedDescription)]
)
}
return result
}
/// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#body
private func encodeResponse(result: GraphQLResult, request: Request, context: Context) throws
-> Response
{
let acceptHeader = request.headers[.accept]
if !config.allowMissingAcceptHeader, acceptHeader == nil {
throw HTTPError(.notAcceptable, message: "An `Accept` header must be provided")
}
let acceptedTypes = parseAcceptHeader(acceptHeader)
// Try to respond with the best matching media type, in order
for mediaType in acceptedTypes {
if MediaType.applicationJsonGraphQL.isType(mediaType) {
return try config.coders.graphQLJSONEncoder.encode(
result,
from: request,
context: context
)
}
if MediaType.applicationJson.isType(mediaType) {
return try config.coders.jsonEncoder.encode(result, from: request, context: context)
}
if MediaType.applicationUrlEncoded.isType(mediaType) {
return try config.coders.urlEncodedFormEncoder.encode(
result,
from: request,
context: context
)
}
}
// Use the default if configured to do so
if config.allowMissingAcceptHeader {
return try config.coders.graphQLJSONEncoder.encode(
result,
from: request,
context: context
)
}
// Fail
throw HTTPError(.notAcceptable)
}
private func parseAcceptHeader(_ header: String?) -> [MediaType] {
guard let header = header else { return [] }
return
header
.split(separator: ",")
.compactMap { segment in
MediaType(from: segment.trimmingCharacters(in: .whitespaces))
}
}
private static func operationTypeOf(
document: Document,
named operationName: String? = nil
) throws -> OperationType {
let operationDefinitions = document.definitions.filter { operation in
operation.kind == .operationDefinition
}.compactMap { operation in
operation as? OperationDefinition
}
// We don't have to validate the document and check for multiple/missing operations here
// That will be done upon execution.
let operationMatch = operationDefinitions.first { operation in
if let operationName {
return operationName == operation.name?.value
} else {
return true
}
}
return operationMatch?.operation ?? .query
}
}