@@ -19,9 +19,9 @@ use std::{
19
19
20
20
use futures:: { future, stream, FutureExt as _, Stream , StreamExt as _, TryFutureExt as _} ;
21
21
use juniper:: {
22
- http:: { GraphQLRequest , GraphQLResponse } ,
23
- BoxFuture , ExecutionError , GraphQLError , GraphQLSubscriptionType , GraphQLTypeAsync , Object ,
24
- ScalarValue , SubscriptionConnection , SubscriptionCoordinator , Value , ValuesStream ,
22
+ http:: GraphQLRequest , BoxFuture , ExecutionError , ExecutionOutput , GraphQLError ,
23
+ GraphQLSubscriptionType , GraphQLTypeAsync , Object , ScalarValue , SubscriptionConnection ,
24
+ SubscriptionCoordinator , Value , ValuesStream ,
25
25
} ;
26
26
27
27
/// Simple [`SubscriptionCoordinator`] implementation:
88
88
89
89
/// Simple [`SubscriptionConnection`] implementation.
90
90
///
91
- /// Resolves `Value<ValuesStream>` into `Stream<Item = GraphQLResponse> ` using the following
92
- /// logic:
91
+ /// Resolves `Value<ValuesStream>` into `Stream<Item = ExecutionOutput<S>> ` using
92
+ /// the following logic:
93
93
///
94
94
/// [`Value::Null`] - returns [`Value::Null`] once
95
95
/// [`Value::Scalar`] - returns `Ok` value or [`Value::Null`] and errors vector
98
98
/// [`Value::Object`] - waits while each field of the [`Object`] is returned, then yields the whole object
99
99
/// `Value::Object<Value::Object<_>>` - returns [`Value::Null`] if [`Value::Object`] consists of sub-objects
100
100
pub struct Connection < ' a , S > {
101
- stream : Pin < Box < dyn Stream < Item = GraphQLResponse < ' a , S > > + Send + ' a > > ,
101
+ stream : Pin < Box < dyn Stream < Item = ExecutionOutput < S > > + Send + ' a > > ,
102
102
}
103
103
104
104
impl < ' a , S > Connection < ' a , S >
@@ -113,16 +113,13 @@ where
113
113
}
114
114
}
115
115
116
- impl < ' a , S > SubscriptionConnection < ' a , S > for Connection < ' a , S > where
117
- S : ScalarValue + Send + Sync + ' a
118
- {
119
- }
116
+ impl < ' a , S > SubscriptionConnection < S > for Connection < ' a , S > where S : ScalarValue + Send + Sync + ' a { }
120
117
121
118
impl < ' a , S > Stream for Connection < ' a , S >
122
119
where
123
120
S : ScalarValue + Send + Sync + ' a ,
124
121
{
125
- type Item = GraphQLResponse < ' a , S > ;
122
+ type Item = ExecutionOutput < S > ;
126
123
127
124
fn poll_next ( self : Pin < & mut Self > , cx : & mut task:: Context < ' _ > ) -> Poll < Option < Self :: Item > > {
128
125
// this is safe as stream is only mutated here and is not moved anywhere
@@ -132,7 +129,7 @@ where
132
129
}
133
130
}
134
131
135
- /// Creates [`futures::Stream`] that yields [`GraphQLResponse`] s depending on the given [`Value`]:
132
+ /// Creates [`futures::Stream`] that yields `ExecutionOutput<S>` s depending on the given [`Value`]:
136
133
///
137
134
/// [`Value::Null`] - returns [`Value::Null`] once
138
135
/// [`Value::Scalar`] - returns `Ok` value or [`Value::Null`] and errors vector
@@ -143,23 +140,28 @@ where
143
140
fn whole_responses_stream < ' a , S > (
144
141
stream : Value < ValuesStream < ' a , S > > ,
145
142
errors : Vec < ExecutionError < S > > ,
146
- ) -> Pin < Box < dyn Stream < Item = GraphQLResponse < ' a , S > > + Send + ' a > >
143
+ ) -> Pin < Box < dyn Stream < Item = ExecutionOutput < S > > + Send + ' a > >
147
144
where
148
145
S : ScalarValue + Send + Sync + ' a ,
149
146
{
150
147
if !errors. is_empty ( ) {
151
- return Box :: pin ( stream:: once ( future:: ready ( GraphQLResponse :: from_result (
152
- Ok ( ( Value :: Null , errors) ) ,
153
- ) ) ) ) ;
148
+ return stream:: once ( future:: ready ( ExecutionOutput {
149
+ data : Value :: null ( ) ,
150
+ errors,
151
+ } ) )
152
+ . boxed ( ) ;
154
153
}
155
154
156
155
match stream {
157
- Value :: Null => Box :: pin ( stream:: once ( future:: ready ( GraphQLResponse :: from_result (
158
- Ok ( ( Value :: Null , vec ! [ ] ) ) ,
156
+ Value :: Null => Box :: pin ( stream:: once ( future:: ready ( ExecutionOutput :: from_data (
157
+ Value :: null ( ) ,
159
158
) ) ) ) ,
160
159
Value :: Scalar ( s) => Box :: pin ( s. map ( |res| match res {
161
- Ok ( val) => GraphQLResponse :: from_result ( Ok ( ( val, vec ! [ ] ) ) ) ,
162
- Err ( err) => GraphQLResponse :: from_result ( Ok ( ( Value :: Null , vec ! [ err] ) ) ) ,
160
+ Ok ( val) => ExecutionOutput :: from_data ( val) ,
161
+ Err ( err) => ExecutionOutput {
162
+ data : Value :: null ( ) ,
163
+ errors : vec ! [ err] ,
164
+ } ,
163
165
} ) ) ,
164
166
Value :: List ( list) => {
165
167
let mut streams = vec ! [ ] ;
@@ -171,9 +173,8 @@ where
171
173
Value :: Object ( mut object) => {
172
174
let obj_len = object. field_count ( ) ;
173
175
if obj_len == 0 {
174
- return Box :: pin ( stream:: once ( future:: ready ( GraphQLResponse :: from_result (
175
- Ok ( ( Value :: Null , vec ! [ ] ) ) ,
176
- ) ) ) ) ;
176
+ return stream:: once ( future:: ready ( ExecutionOutput :: from_data ( Value :: null ( ) ) ) )
177
+ . boxed ( ) ;
177
178
}
178
179
179
180
let mut filled_count = 0 ;
@@ -182,7 +183,7 @@ where
182
183
ready_vec. push ( None ) ;
183
184
}
184
185
185
- let stream = stream:: poll_fn ( move |mut ctx| -> Poll < Option < GraphQLResponse < ' a , S > > > {
186
+ let stream = stream:: poll_fn ( move |mut ctx| -> Poll < Option < ExecutionOutput < S > > > {
186
187
let mut obj_iterator = object. iter_mut ( ) ;
187
188
188
189
// Due to having to modify `ready_vec` contents (by-move pattern)
@@ -233,10 +234,7 @@ where
233
234
}
234
235
} ) ;
235
236
let obj = Object :: from_iter ( ready_vec_iterator) ;
236
- Poll :: Ready ( Some ( GraphQLResponse :: from_result ( Ok ( (
237
- Value :: Object ( obj) ,
238
- vec ! [ ] ,
239
- ) ) ) ) )
237
+ Poll :: Ready ( Some ( ExecutionOutput :: from_data ( Value :: Object ( obj) ) ) )
240
238
} else {
241
239
Poll :: Pending
242
240
}
@@ -256,9 +254,13 @@ mod whole_responses_stream {
256
254
257
255
#[ tokio:: test]
258
256
async fn with_error ( ) {
259
- let expected = vec ! [ GraphQLResponse :: <DefaultScalarValue >:: error(
260
- FieldError :: new( "field error" , Value :: Null ) ,
261
- ) ] ;
257
+ let expected = vec ! [ ExecutionOutput {
258
+ data: Value :: <DefaultScalarValue >:: Null ,
259
+ errors: vec![ ExecutionError :: at_origin( FieldError :: new(
260
+ "field error" ,
261
+ Value :: Null ,
262
+ ) ) ] ,
263
+ } ] ;
262
264
let expected = serde_json:: to_string ( & expected) . unwrap ( ) ;
263
265
264
266
let result = whole_responses_stream :: < DefaultScalarValue > (
@@ -277,10 +279,9 @@ mod whole_responses_stream {
277
279
278
280
#[ tokio:: test]
279
281
async fn value_null ( ) {
280
- let expected = vec ! [ GraphQLResponse :: <DefaultScalarValue >:: from_result( Ok ( (
281
- Value :: Null ,
282
- vec![ ] ,
283
- ) ) ) ] ;
282
+ let expected = vec ! [ ExecutionOutput :: from_data(
283
+ Value :: <DefaultScalarValue >:: Null ,
284
+ ) ] ;
284
285
let expected = serde_json:: to_string ( & expected) . unwrap ( ) ;
285
286
286
287
let result = whole_responses_stream :: < DefaultScalarValue > ( Value :: Null , vec ! [ ] )
@@ -296,26 +297,11 @@ mod whole_responses_stream {
296
297
#[ tokio:: test]
297
298
async fn value_scalar ( ) {
298
299
let expected = vec ! [
299
- GraphQLResponse :: from_result( Ok ( (
300
- Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ,
301
- vec![ ] ,
302
- ) ) ) ,
303
- GraphQLResponse :: from_result( Ok ( (
304
- Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ,
305
- vec![ ] ,
306
- ) ) ) ,
307
- GraphQLResponse :: from_result( Ok ( (
308
- Value :: Scalar ( DefaultScalarValue :: Int ( 3i32 ) ) ,
309
- vec![ ] ,
310
- ) ) ) ,
311
- GraphQLResponse :: from_result( Ok ( (
312
- Value :: Scalar ( DefaultScalarValue :: Int ( 4i32 ) ) ,
313
- vec![ ] ,
314
- ) ) ) ,
315
- GraphQLResponse :: from_result( Ok ( (
316
- Value :: Scalar ( DefaultScalarValue :: Int ( 5i32 ) ) ,
317
- vec![ ] ,
318
- ) ) ) ,
300
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ) ,
301
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ) ,
302
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 3i32 ) ) ) ,
303
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 4i32 ) ) ) ,
304
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 5i32 ) ) ) ,
319
305
] ;
320
306
let expected = serde_json:: to_string ( & expected) . unwrap ( ) ;
321
307
@@ -340,19 +326,10 @@ mod whole_responses_stream {
340
326
#[ tokio:: test]
341
327
async fn value_list ( ) {
342
328
let expected = vec ! [
343
- GraphQLResponse :: from_result( Ok ( (
344
- Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ,
345
- vec![ ] ,
346
- ) ) ) ,
347
- GraphQLResponse :: from_result( Ok ( (
348
- Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ,
349
- vec![ ] ,
350
- ) ) ) ,
351
- GraphQLResponse :: from_result( Ok ( ( Value :: Null , vec![ ] ) ) ) ,
352
- GraphQLResponse :: from_result( Ok ( (
353
- Value :: Scalar ( DefaultScalarValue :: Int ( 4i32 ) ) ,
354
- vec![ ] ,
355
- ) ) ) ,
329
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ) ,
330
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ) ,
331
+ ExecutionOutput :: from_data( Value :: null( ) ) ,
332
+ ExecutionOutput :: from_data( Value :: Scalar ( DefaultScalarValue :: Int ( 4i32 ) ) ) ,
356
333
] ;
357
334
let expected = serde_json:: to_string ( & expected) . unwrap ( ) ;
358
335
@@ -380,25 +357,19 @@ mod whole_responses_stream {
380
357
#[ tokio:: test]
381
358
async fn value_object ( ) {
382
359
let expected = vec ! [
383
- GraphQLResponse :: from_result( Ok ( (
384
- Value :: Object ( Object :: from_iter(
385
- vec![
386
- ( "one" , Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ) ,
387
- ( "two" , Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ) ,
388
- ]
389
- . into_iter( ) ,
390
- ) ) ,
391
- vec![ ] ,
360
+ ExecutionOutput :: from_data( Value :: Object ( Object :: from_iter(
361
+ vec![
362
+ ( "one" , Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ) ,
363
+ ( "two" , Value :: Scalar ( DefaultScalarValue :: Int ( 1i32 ) ) ) ,
364
+ ]
365
+ . into_iter( ) ,
392
366
) ) ) ,
393
- GraphQLResponse :: from_result( Ok ( (
394
- Value :: Object ( Object :: from_iter(
395
- vec![
396
- ( "one" , Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ) ,
397
- ( "two" , Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ) ,
398
- ]
399
- . into_iter( ) ,
400
- ) ) ,
401
- vec![ ] ,
367
+ ExecutionOutput :: from_data( Value :: Object ( Object :: from_iter(
368
+ vec![
369
+ ( "one" , Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ) ,
370
+ ( "two" , Value :: Scalar ( DefaultScalarValue :: Int ( 2i32 ) ) ) ,
371
+ ]
372
+ . into_iter( ) ,
402
373
) ) ) ,
403
374
] ;
404
375
let expected = serde_json:: to_string ( & expected) . unwrap ( ) ;
0 commit comments