@@ -4,12 +4,17 @@ import GraphQL
44/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization.
55///
66/// By default, there are no authorization checks
7- public class Server < InitPayload: Equatable & Codable > : @unchecked Sendable {
7+ public class Server <
8+ InitPayload: Equatable & Codable & Sendable ,
9+ SubscriptionSequenceType: AsyncSequence & Sendable
10+ > : @unchecked Sendable where
11+ SubscriptionSequenceType. Element == GraphQLResult
12+ {
813 // We keep this weak because we strongly inject this object into the messenger callback
914 weak var messenger : Messenger ?
1015
1116 let onExecute : ( GraphQLRequest ) async throws -> GraphQLResult
12- let onSubscribe : ( GraphQLRequest ) async throws -> Result < AsyncThrowingStream < GraphQLResult , Error > , GraphQLErrors >
17+ let onSubscribe : ( GraphQLRequest ) async throws -> SubscriptionSequenceType
1318 var auth : ( InitPayload ) async throws -> Void
1419
1520 var onExit : ( ) async throws -> Void = { }
@@ -33,7 +38,7 @@ public class Server<InitPayload: Equatable & Codable>: @unchecked Sendable {
3338 public init (
3439 messenger: Messenger ,
3540 onExecute: @escaping ( GraphQLRequest ) async throws -> GraphQLResult ,
36- onSubscribe: @escaping ( GraphQLRequest ) async throws -> Result < AsyncThrowingStream < GraphQLResult , Error > , GraphQLErrors >
41+ onSubscribe: @escaping ( GraphQLRequest ) async throws -> SubscriptionSequenceType
3742 ) {
3843 self . messenger = messenger
3944 self . onExecute = onExecute
@@ -166,16 +171,9 @@ public class Server<InitPayload: Equatable & Codable>: @unchecked Sendable {
166171 }
167172
168173 if isStreaming {
169- do {
170- let result = try await onSubscribe ( graphQLRequest)
171- let stream : AsyncThrowingStream < GraphQLResult , Error >
174+ subscriptionTasks [ id] = Task {
172175 do {
173- stream = try result. get ( )
174- } catch {
175- try await sendError ( error, id: id)
176- return
177- }
178- subscriptionTasks [ id] = Task {
176+ let stream = try await onSubscribe ( graphQLRequest)
179177 for try await event in stream {
180178 try Task . checkCancellation ( )
181179 do {
@@ -185,10 +183,11 @@ public class Server<InitPayload: Equatable & Codable>: @unchecked Sendable {
185183 throw error
186184 }
187185 }
188- try await self . sendComplete ( id: id)
186+ } catch {
187+ try await sendError ( error, id: id)
188+ throw error
189189 }
190- } catch {
191- try await sendError ( error, id: id)
190+ try await self . sendComplete ( id: id)
192191 }
193192 } else {
194193 do {
0 commit comments