-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorld.swift
More file actions
82 lines (75 loc) · 2.79 KB
/
Copy pathHelloWorld.swift
File metadata and controls
82 lines (75 loc) · 2.79 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
import AsyncAlgorithms
import GraphQL
import GraphQLHummingbird
import Hummingbird
import HummingbirdWebSocket
import Logging
@main
struct HelloWorld {
static func main() async throws {
let schema = try GraphQLSchema(
query: GraphQLObjectType(
name: "Query",
fields: [
"hello": GraphQLField(
type: GraphQLString,
resolve: { _, _, _, _ in
"World"
}
),
]
),
subscription: GraphQLObjectType(
name: "Subscription",
fields: [
"hello": GraphQLField(
type: GraphQLString,
description: "Emits an updated `World` message every 3 seconds",
resolve: { eventResult, _, _, _ in
eventResult
},
subscribe: { _, _, _, _ in
let clock = ContinuousClock()
let start = clock.now
return AsyncTimerSequence(interval: .seconds(3), clock: ContinuousClock()).map { instant in
"World at \(start.duration(to: instant))"
}
}
),
]
)
)
let router = Router(context: HummingbirdContext.self)
router.graphql(schema: schema, config: .init(allowMissingAcceptHeader: true)) { _ in
GraphQLContext()
}
let webSocketRouter = Router(context: HummingbirdWebSocketContext.self)
webSocketRouter.graphqlWebSocket(schema: schema) { _ in
GraphQLContext()
}
let app = Application(
router: router,
server: .http1WebSocketUpgrade(webSocketRouter: webSocketRouter)
)
try await app.runService()
}
struct GraphQLContext: @unchecked Sendable {}
struct HummingbirdContext: RequestContext {
var coreContext: Hummingbird.CoreRequestContextStorage
var logger: Logging.Logger
init(source: Hummingbird.ApplicationRequestContextSource) {
coreContext = .init(source: source)
logger = source.logger
}
}
struct HummingbirdWebSocketContext: WebSocketRequestContext, RequestContext {
var coreContext: Hummingbird.CoreRequestContextStorage
var webSocket: HummingbirdWebSocket.WebSocketHandlerReference<Self>
var logger: Logging.Logger
init(source: Hummingbird.ApplicationRequestContextSource) {
coreContext = .init(source: source)
webSocket = .init()
logger = source.logger
}
}
}