@@ -4,12 +4,18 @@ 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 ,
9+ SubscriptionSequenceType: AsyncSequence
10+ > : @unchecked Sendable where
11+ SubscriptionSequenceType. Element == GraphQLResult
12+ {
13+
814 // We keep this weak because we strongly inject this object into the messenger callback
915 weak var messenger : Messenger ?
1016
1117 let onExecute : ( GraphQLRequest ) async throws -> GraphQLResult
12- let onSubscribe : ( GraphQLRequest ) async throws -> Result < AsyncThrowingStream < GraphQLResult , Error > , GraphQLErrors >
18+ let onSubscribe : ( GraphQLRequest ) async throws -> SubscriptionSequenceType
1319 var auth : ( InitPayload ) async throws -> Void
1420
1521 var onExit : ( ) async throws -> Void = { }
@@ -33,7 +39,7 @@ public class Server<InitPayload: Equatable & Codable>: @unchecked Sendable {
3339 public init (
3440 messenger: Messenger ,
3541 onExecute: @escaping ( GraphQLRequest ) async throws -> GraphQLResult ,
36- onSubscribe: @escaping ( GraphQLRequest ) async throws -> Result < AsyncThrowingStream < GraphQLResult , Error > , GraphQLErrors >
42+ onSubscribe: @escaping ( GraphQLRequest ) async throws -> SubscriptionSequenceType
3743 ) {
3844 self . messenger = messenger
3945 self . onExecute = onExecute
@@ -160,16 +166,9 @@ public class Server<InitPayload: Equatable & Codable>: @unchecked Sendable {
160166 }
161167
162168 if isStreaming {
163- do {
164- let result = try await onSubscribe ( graphQLRequest)
165- let stream : AsyncThrowingStream < GraphQLResult , Error >
169+ subscriptionTasks [ id] = Task {
166170 do {
167- stream = try result. get ( )
168- } catch {
169- try await sendError ( error, id: id)
170- return
171- }
172- subscriptionTasks [ id] = Task {
171+ let stream = try await onSubscribe ( graphQLRequest)
173172 for try await event in stream {
174173 try Task . checkCancellation ( )
175174 do {
@@ -179,10 +178,11 @@ public class Server<InitPayload: Equatable & Codable>: @unchecked Sendable {
179178 throw error
180179 }
181180 }
182- try await self . sendComplete ( id: id)
181+ } catch {
182+ try await sendError ( error, id: id)
183+ throw error
183184 }
184- } catch {
185- try await sendError ( error, id: id)
185+ try await self . sendComplete ( id: id)
186186 }
187187 } else {
188188 do {
0 commit comments