-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphQLConfig.swift
More file actions
143 lines (127 loc) · 5.26 KB
/
Copy pathGraphQLConfig.swift
File metadata and controls
143 lines (127 loc) · 5.26 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
import Foundation
import GraphQL
import Hummingbird
/// Configuration options for GraphQLHummingbird
public struct GraphQLConfig<
Context: RequestContext,
WebSocketInit: Equatable & Codable & Sendable,
WebSocketInitResult: Sendable
>: Sendable {
let allowGet: Bool
let allowMissingAcceptHeader: Bool
let coders: Coders
let ide: IDE
let subscriptionProtocols: Set<SubscriptionProtocol>
let websocket: WebSocket
let additionalValidationRules: [@Sendable (ValidationContext) -> Visitor]
/// Configuration options for GraphQLHummingbird
/// - Parameters:
/// - allowGet: Whether to allow GraphQL queries via `GET` requests.
/// - allowMissingAcceptHeader: Whether to allow clients to omit "Accept" headers and default to `application/graphql-response+json` encoded responses.
/// - ide: The IDE to expose
/// - subscriptionProtocols: Protocols used to support GraphQL subscription requests
/// - websocket: WebSocket-specific configuration
/// - additionalValidationRules: Additional validation rules to apply to requests. The default GraphQL validation rules are always applied.
public init(
allowGet: Bool = true,
allowMissingAcceptHeader: Bool = false,
coders: Coders = .init(),
ide: IDE = .graphiql,
subscriptionProtocols: Set<SubscriptionProtocol> = [.websocket],
websocket: WebSocket = .init(
// Including this strongly-typed argument is required to avoid compiler failures on Swift 6.2.3.
onWebSocketInit: { (_: EmptyWebSocketInit, _: Request, _: Context) in }
),
additionalValidationRules: [@Sendable (ValidationContext) -> Visitor] = []
) {
self.allowGet = allowGet
self.allowMissingAcceptHeader = allowMissingAcceptHeader
self.additionalValidationRules = additionalValidationRules
self.coders = coders
self.ide = ide
self.subscriptionProtocols = subscriptionProtocols
self.websocket = websocket
}
public struct Coders: Sendable {
public let graphQLJSONEncoder: GraphQLJSONEncoder
public let jsonDecoder: JSONDecoder
public let jsonEncoder: JSONEncoder
public let urlEncodedFormDecoder: URLEncodedFormDecoder
public let urlEncodedFormEncoder: URLEncodedFormEncoder
public init(
graphQLJSONEncoder: GraphQLJSONEncoder? = nil,
jsonDecoder: JSONDecoder? = nil,
jsonEncoder: JSONEncoder? = nil,
urlEncodedFormDecoder: URLEncodedFormDecoder? = nil,
urlEncodedFormEncoder: URLEncodedFormEncoder? = nil
) {
self.graphQLJSONEncoder = graphQLJSONEncoder ?? defaultGraphQLJSONEncoder
self.jsonDecoder = jsonDecoder ?? defaultJSONDecoder
self.jsonEncoder = jsonEncoder ?? defaultJSONEncoder
self.urlEncodedFormDecoder = urlEncodedFormDecoder ?? defaultURLEncodedFormDecoder
self.urlEncodedFormEncoder = urlEncodedFormEncoder ?? defaultURLEncodedFormEncoder
}
}
public struct IDE: Sendable, Equatable {
/// GraphiQL: https://github.com/graphql/graphiql
public static var graphiql: Self {
.init(type: .graphiql)
}
/// Do not expose a GraphQL IDE
public static var none: Self {
.init(type: .none)
}
let type: IDEType
enum IDEType {
case graphiql
case none
}
}
public struct SubscriptionProtocol: Sendable, Hashable {
/// Expose GraphQL subscriptions over WebSockets
public static var websocket: Self {
.init(type: .websocket)
}
let type: SubscriptionProtocolType
enum SubscriptionProtocolType {
case websocket
}
}
public struct WebSocket: Sendable {
let onWebSocketInit: @Sendable (WebSocketInit, Request, Context) async throws -> WebSocketInitResult
/// GraphQL over WebSocket configuration
/// - Parameter onWebSocketInit: A custom callback run during `connection_init` resolution that allows
/// authorization using the `payload` field of the `connection_init` message.
/// Throw from this closure to indicate that authorization has failed.
public init(
onWebSocketInit: @Sendable @escaping (WebSocketInit, Request, Context) async throws -> WebSocketInitResult = { (_: EmptyWebSocketInit, _: Request, _: Context) in }
) {
self.onWebSocketInit = onWebSocketInit
}
}
}
let defaultGraphQLJSONEncoder: GraphQLJSONEncoder = {
let encoder = GraphQLJSONEncoder()
encoder.dateEncodingStrategy = .iso8601
return encoder
}()
let defaultJSONDecoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
let defaultJSONEncoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
return encoder
}()
let defaultURLEncodedFormDecoder: URLEncodedFormDecoder = {
var decoder = URLEncodedFormDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
let defaultURLEncodedFormEncoder: URLEncodedFormEncoder = {
var encoder = URLEncodedFormEncoder()
encoder.dateEncodingStrategy = .iso8601
return encoder
}()