@@ -51,118 +51,133 @@ public actor Client<InitPayload: Equatable & Codable> {
5151 /// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
5252 /// - Parameter incoming: The server message sequence that the client should react to.
5353 public func listen< A: AsyncSequence & Sendable > ( to incoming: A ) async throws
54- where A. Element == String {
54+ where A. Element == Data {
5555 for try await message in incoming {
56- // Detect and ignore error responses.
57- if message. starts ( with: " 44 " ) {
58- // TODO: Determine what to do with returned error messages
56+ try await respond ( to: message)
57+ }
58+ }
59+
60+ /// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
61+ /// - Parameter incoming: The server message sequence that the client should react to.
62+ @available ( * , deprecated, message: " Use `Data` sequence instead. " )
63+ public func listen< A: AsyncSequence & Sendable > ( to incoming: A ) async throws
64+ where A. Element == String {
65+ for try await stringMessage in incoming {
66+ guard let message = stringMessage. data ( using: . utf8) else {
67+ try await self . error ( . invalidEncoding( ) )
5968 return
6069 }
70+ try await respond ( to: message)
71+ }
72+ }
6173
62- guard let json = message. data ( using: . utf8) else {
63- try await error ( . invalidEncoding( ) )
74+ private func respond( to message: Data ) async throws {
75+ let response : Response
76+ do {
77+ response = try decoder. decode ( Response . self, from: message)
78+ } catch {
79+ try await self . error ( . noType( ) )
80+ return
81+ }
82+
83+ switch response. type {
84+ case . GQL_CONNECTION_ERROR:
85+ guard
86+ let connectionErrorResponse = try ? decoder. decode (
87+ ConnectionErrorResponse . self,
88+ from: message
89+ )
90+ else {
91+ try await error ( . invalidResponseFormat( messageType: . GQL_CONNECTION_ERROR) )
6492 return
6593 }
66-
67- let response : Response
68- do {
69- response = try decoder. decode ( Response . self, from: json)
70- } catch {
71- try await self . error ( . noType( ) )
94+ try await onConnectionError ( connectionErrorResponse, self )
95+ case . GQL_CONNECTION_ACK:
96+ guard
97+ let connectionAckResponse = try ? decoder. decode (
98+ ConnectionAckResponse . self,
99+ from: message
100+ )
101+ else {
102+ try await error ( . invalidResponseFormat( messageType: . GQL_CONNECTION_ERROR) )
72103 return
73104 }
74-
75- switch response. type {
76- case . GQL_CONNECTION_ERROR:
77- guard
78- let connectionErrorResponse = try ? decoder. decode (
79- ConnectionErrorResponse . self,
80- from: json
81- )
82- else {
83- try await error ( . invalidResponseFormat( messageType: . GQL_CONNECTION_ERROR) )
84- return
85- }
86- try await onConnectionError ( connectionErrorResponse, self )
87- case . GQL_CONNECTION_ACK:
88- guard
89- let connectionAckResponse = try ? decoder. decode (
90- ConnectionAckResponse . self,
91- from: json
92- )
93- else {
94- try await error ( . invalidResponseFormat( messageType: . GQL_CONNECTION_ERROR) )
95- return
96- }
97- try await onConnectionAck ( connectionAckResponse, self )
98- case . GQL_CONNECTION_KEEP_ALIVE:
99- guard
100- let connectionKeepAliveResponse = try ? decoder. decode (
101- ConnectionKeepAliveResponse . self,
102- from: json
103- )
104- else {
105- try await error ( . invalidResponseFormat( messageType: . GQL_CONNECTION_KEEP_ALIVE) )
106- return
107- }
108- try await onConnectionKeepAlive ( connectionKeepAliveResponse, self )
109- case . GQL_DATA:
110- guard let nextResponse = try ? decoder. decode ( DataResponse . self, from: json) else {
111- try await error ( . invalidResponseFormat( messageType: . GQL_DATA) )
112- return
113- }
114- try await onData ( nextResponse, self )
115- case . GQL_ERROR:
116- guard let errorResponse = try ? decoder. decode ( ErrorResponse . self, from: json) else {
117- try await error ( . invalidResponseFormat( messageType: . GQL_ERROR) )
118- return
119- }
120- try await onError ( errorResponse, self )
121- case . GQL_COMPLETE:
122- guard let completeResponse = try ? decoder. decode ( CompleteResponse . self, from: json)
123- else {
124- try await error ( . invalidResponseFormat( messageType: . GQL_COMPLETE) )
125- return
126- }
127- try await onComplete ( completeResponse, self )
128- default :
129- try await error ( . invalidType( ) )
105+ try await onConnectionAck ( connectionAckResponse, self )
106+ case . GQL_CONNECTION_KEEP_ALIVE:
107+ guard
108+ let connectionKeepAliveResponse = try ? decoder. decode (
109+ ConnectionKeepAliveResponse . self,
110+ from: message
111+ )
112+ else {
113+ try await error ( . invalidResponseFormat( messageType: . GQL_CONNECTION_KEEP_ALIVE) )
114+ return
115+ }
116+ try await onConnectionKeepAlive ( connectionKeepAliveResponse, self )
117+ case . GQL_DATA:
118+ guard let nextResponse = try ? decoder. decode ( DataResponse . self, from: message) else {
119+ try await error ( . invalidResponseFormat( messageType: . GQL_DATA) )
120+ return
121+ }
122+ try await onData ( nextResponse, self )
123+ case . GQL_ERROR:
124+ guard let errorResponse = try ? decoder. decode ( ErrorResponse . self, from: message) else {
125+ try await error ( . invalidResponseFormat( messageType: . GQL_ERROR) )
126+ return
127+ }
128+ try await onError ( errorResponse, self )
129+ case . GQL_COMPLETE:
130+ guard let completeResponse = try ? decoder. decode ( CompleteResponse . self, from: message)
131+ else {
132+ try await error ( . invalidResponseFormat( messageType: . GQL_COMPLETE) )
133+ return
130134 }
135+ try await onComplete ( completeResponse, self )
136+ default :
137+ try await error ( . invalidType( ) )
131138 }
132139 }
133140
134141 /// Send a `connection_init` request through the messenger
135142 public func sendConnectionInit( payload: InitPayload ) async throws {
136143 try await messenger. send (
137- ConnectionInitRequest (
138- payload: payload
139- ) . toJSON ( encoder)
144+ encoder. encode (
145+ ConnectionInitRequest (
146+ payload: payload
147+ )
148+ )
140149 )
141150 }
142151
143152 /// Send a `start` request through the messenger
144153 public func sendStart( payload: GraphQLRequest , id: String ) async throws {
145154 try await messenger. send (
146- StartRequest (
147- payload: payload,
148- id: id
149- ) . toJSON ( encoder)
155+ encoder. encode (
156+ StartRequest (
157+ payload: payload,
158+ id: id
159+ )
160+ )
150161 )
151162 }
152163
153164 /// Send a `stop` request through the messenger
154165 public func sendStop( id: String ) async throws {
155166 try await messenger. send (
156- StopRequest (
157- id: id
158- ) . toJSON ( encoder)
167+ encoder. encode (
168+ StopRequest (
169+ id: id
170+ )
171+ )
159172 )
160173 }
161174
162175 /// Send a `connection_terminate` request through the messenger
163176 public func sendConnectionTerminate( ) async throws {
164177 try await messenger. send (
165- ConnectionTerminateRequest ( ) . toJSON ( encoder)
178+ encoder. encode (
179+ ConnectionTerminateRequest ( )
180+ )
166181 )
167182 }
168183
0 commit comments