2525
2626 private var initialized = false
2727 private var initResult : InitPayloadResult ?
28- private var subscriptionTasks = [ String : Task < Void , any Error > ] ( )
28+ private var executionTasks = [ String : Task < Void , any Error > ] ( )
2929
3030 /// Create a new server
3131 ///
5353 }
5454
5555 deinit {
56- subscriptionTasks . values. forEach { $0. cancel ( ) }
56+ executionTasks . values. forEach { $0. cancel ( ) }
5757 }
5858
5959 /// Listen and react to the provided async sequence of client messages. This function will block until the stream is completed.
@@ -70,39 +70,44 @@ where
7070 do {
7171 request = try decoder. decode ( Request . self, from: message)
7272 } catch {
73- try await self . error ( . noType( ) )
73+ try await messenger . error ( . noType( ) )
7474 return
7575 }
7676
7777 // handle incoming message
7878 switch request. type {
7979 case . connectionInit:
80- guard
81- let connectionInitRequest = try ? decoder. decode (
80+ let connectionInitRequest : ConnectionInitRequest < InitPayload >
81+ do {
82+ connectionInitRequest = try decoder. decode (
8283 ConnectionInitRequest< InitPayload> . self ,
8384 from: message
8485 )
85- else {
86- try await error ( . invalidRequestFormat( messageType: . connectionInit) )
86+ } catch {
87+ try await messenger . error ( . invalidRequestFormat( messageType: . connectionInit, error : error ) )
8788 return
8889 }
8990 try await onConnectionInit ( connectionInitRequest, messenger)
9091 case . subscribe:
91- guard let subscribeRequest = try ? decoder. decode ( SubscribeRequest . self, from: message)
92- else {
93- try await error ( . invalidRequestFormat( messageType: . subscribe) )
92+ let subscribeRequest : SubscribeRequest
93+ do {
94+ subscribeRequest = try decoder. decode ( SubscribeRequest . self, from: message)
95+ } catch {
96+ try await messenger. error ( . invalidRequestFormat( messageType: . subscribe, error: error) )
9497 return
9598 }
9699 try await onSubscribe ( subscribeRequest)
97100 case . complete:
98- guard let completeRequest = try ? decoder. decode ( CompleteRequest . self, from: message)
99- else {
100- try await error ( . invalidRequestFormat( messageType: . complete) )
101+ let completeRequest : CompleteRequest
102+ do {
103+ completeRequest = try decoder. decode ( CompleteRequest . self, from: message)
104+ } catch {
105+ try await messenger. error ( . invalidRequestFormat( messageType: . complete, error: error) )
101106 return
102107 }
103108 try await onOperationComplete ( completeRequest)
104109 default :
105- try await error ( . invalidType( ) )
110+ try await messenger . error ( . invalidType( ) )
106111 }
107112 }
108113
@@ -111,14 +116,14 @@ where
111116 _: Messenger
112117 ) async throws {
113118 guard !initialized else {
114- try await error ( . tooManyInitializations( ) )
119+ try await messenger . error ( . tooManyInitializations( ) )
115120 return
116121 }
117122
118123 do {
119124 initResult = try await onInit ( connectionInitRequest. payload)
120125 } catch {
121- try await self . error ( . forbidden( ) )
126+ try await messenger . error ( . forbidden( ) )
122127 return
123128 }
124129 initialized = true
@@ -128,62 +133,66 @@ where
128133
129134 private func onSubscribe( _ subscribeRequest: SubscribeRequest ) async throws {
130135 guard initialized, let initResult else {
131- try await error ( . notInitialized( ) )
136+ try await messenger . error ( . notInitialized( ) )
132137 return
133138 }
134139
135140 let id = subscribeRequest. id
136- if subscriptionTasks [ id] != nil {
137- try await error ( . subscriberAlreadyExists( id: id) )
138- }
139-
140141 let graphQLRequest = subscribeRequest. payload
141142
142- var isStreaming = false
143+ let isStreaming : Bool
143144 do {
144145 isStreaming = try graphQLRequest. isSubscription ( )
145146 } catch {
146147 try await sendError ( error, id: id)
147148 return
148149 }
149150
150- if isStreaming {
151- subscriptionTasks [ id] = Task {
151+ guard executionTasks [ id] == nil else {
152+ try await messenger. error ( . subscriberAlreadyExists( id: id) )
153+ return
154+ }
155+ executionTasks [ id] = Task {
156+ defer {
157+ executionTasks. removeValue ( forKey: id)
158+ }
159+
160+ if isStreaming {
161+ let stream : SubscriptionSequenceType
152162 do {
153- let stream = try await onSubscribe ( graphQLRequest, initResult)
154- for try await event in stream {
155- try Task . checkCancellation ( )
156- try await self . sendNext ( event, id: id)
157- }
163+ stream = try await onSubscribe ( graphQLRequest, initResult)
158164 } catch {
159165 try await sendError ( error, id: id)
160- subscriptionTasks. removeValue ( forKey: id)
161- throw error
166+ return
167+ }
168+ for try await event in stream {
169+ try await self . sendNext ( event, id: id)
170+ }
171+ executionTasks. removeValue ( forKey: id)
172+ } else {
173+ let result : GraphQLResult
174+ do {
175+ result = try await onExecute ( graphQLRequest, initResult)
176+ } catch {
177+ try await sendError ( error, id: id)
178+ return
162179 }
163- try await self . sendComplete ( id: id)
164- subscriptionTasks. removeValue ( forKey: id)
165- }
166- } else {
167- do {
168- let result = try await onExecute ( graphQLRequest, initResult)
169180 try await sendNext ( result, id: id)
170- try await sendComplete ( id: id)
171- } catch {
172- try await sendError ( error, id: id)
173181 }
182+ try await sendComplete ( id: id)
174183 }
175184 }
176185
177186 private func onOperationComplete( _ completeRequest: CompleteRequest ) async throws {
178187 guard initialized else {
179- try await error ( . notInitialized( ) )
188+ try await messenger . error ( . notInitialized( ) )
180189 return
181190 }
182191
183192 let id = completeRequest. id
184- if let task = subscriptionTasks [ id] {
193+ if let task = executionTasks [ id] {
185194 task. cancel ( )
186- subscriptionTasks . removeValue ( forKey: id)
195+ executionTasks . removeValue ( forKey: id)
187196 }
188197 try await onOperationComplete ( id)
189198 }
@@ -238,14 +247,4 @@ where
238247 private func sendError( _ error: Error , id: String ) async throws {
239248 try await sendError ( [ error] , id: id)
240249 }
241-
242- /// Send an `error` response through the messenger
243- private func sendError( _ errorMessage: String , id: String ) async throws {
244- try await sendError ( GraphQLError ( message: errorMessage) , id: id)
245- }
246-
247- /// Send an error through the messenger and close the connection
248- private func error( _ error: GraphQLTransportWSError ) async throws {
249- try await messenger. error ( error. message, code: error. code. rawValue)
250- }
251250}
0 commit comments