@@ -8,11 +8,11 @@ use graphql_parser::{
8
8
use juniper:: {
9
9
meta:: { Field , MetaType } ,
10
10
Arguments , ExecutionResult , Executor , FieldError , GraphQLType , GraphQLValue , Registry ,
11
- ScalarValue , Selection , Value ,
11
+ ScalarValue , Selection , Value , GraphQLValueAsync , BoxFuture ,
12
12
} ;
13
13
use serde_json:: Value as Json ;
14
14
15
- use crate :: { types:: base:: resolve_selection_set_into, GraphQLValueAsync } ;
15
+ use crate :: { types:: base:: resolve_selection_set_into} ;
16
16
17
17
// Used to describe the graphql type of a `serde_json::Value` using the GraphQL
18
18
// schema definition language.
@@ -29,8 +29,8 @@ pub struct TypeInfo {
29
29
30
30
impl TypeInfo {
31
31
fn meta < ' r , S > ( & self , registry : & mut Registry < ' r , S > ) -> MetaType < ' r , S >
32
- where
33
- S : ScalarValue + ' r ,
32
+ where
33
+ S : ScalarValue + ' r ,
34
34
{
35
35
let mut fields = Vec :: new ( ) ;
36
36
let s = self . schema . clone ( ) . unwrap_or_default ( ) ;
@@ -67,9 +67,9 @@ impl TypeInfo {
67
67
type_ref : Type < ' t , T > ,
68
68
nullable : bool ,
69
69
) -> Field < ' r , S >
70
- where
71
- S : ' r + ScalarValue ,
72
- T : Text < ' t > ,
70
+ where
71
+ S : ' r + ScalarValue ,
72
+ T : Text < ' t > ,
73
73
{
74
74
match type_ref {
75
75
Type :: NamedType ( type_name) => match type_name. as_ref ( ) {
@@ -115,7 +115,11 @@ impl TypeInfo {
115
115
} ,
116
116
Type :: ListType ( nested_type) => {
117
117
let mut field = self . build_field ( registry, field_name, * nested_type, true ) ;
118
- field. field_type = juniper:: Type :: List ( Box :: new ( field. field_type ) , None ) ;
118
+ if nullable {
119
+ field. field_type = juniper:: Type :: List ( Box :: new ( field. field_type ) , None ) ;
120
+ } else {
121
+ field. field_type = juniper:: Type :: NonNullList ( Box :: new ( field. field_type ) , None ) ;
122
+ }
119
123
field
120
124
}
121
125
Type :: NonNullType ( nested_type) => {
@@ -131,8 +135,8 @@ impl<S: ScalarValue> GraphQLType<S> for Json {
131
135
}
132
136
133
137
fn meta < ' r > ( info : & Self :: TypeInfo , registry : & mut Registry < ' r , S > ) -> MetaType < ' r , S >
134
- where
135
- S : ' r ,
138
+ where
139
+ S : ' r ,
136
140
{
137
141
info. meta ( registry)
138
142
}
@@ -225,6 +229,38 @@ impl<S: ScalarValue> GraphQLValue<S> for Json {
225
229
}
226
230
}
227
231
232
+
233
+ impl < S > GraphQLValueAsync < S > for Json
234
+ where
235
+ Self :: TypeInfo : Sync ,
236
+ Self :: Context : Sync ,
237
+ S : ScalarValue + Send + Sync ,
238
+ {
239
+ fn resolve_async < ' a > (
240
+ & ' a self ,
241
+ info : & ' a Self :: TypeInfo ,
242
+ selection_set : Option < & ' a [ Selection < S > ] > ,
243
+ executor : & ' a Executor < Self :: Context , S > ,
244
+ ) -> BoxFuture < ' a , ExecutionResult < S > > {
245
+ Box :: pin ( async move {
246
+ <Json as GraphQLValue < S > >:: resolve ( self , info, selection_set, executor)
247
+ } )
248
+ }
249
+
250
+ fn resolve_field_async < ' a > (
251
+ & ' a self ,
252
+ info : & ' a Self :: TypeInfo ,
253
+ field_name : & ' a str ,
254
+ arguments : & ' a Arguments < S > ,
255
+ executor : & ' a Executor < Self :: Context , S > ,
256
+ ) -> BoxFuture < ' a , ExecutionResult < S > > {
257
+ Box :: pin ( async move {
258
+ <Json as GraphQLValue < S > >:: resolve_field ( self , info, field_name, arguments, executor)
259
+ } )
260
+ }
261
+ }
262
+
263
+
228
264
#[ cfg( test) ]
229
265
mod tests {
230
266
use juniper:: {
@@ -422,4 +458,50 @@ mod tests {
422
458
) ) ,
423
459
) ;
424
460
}
461
+
462
+ #[ tokio:: test]
463
+ async fn test_async ( ) {
464
+ let sdl = r#"
465
+ type Query {
466
+ hero: Hero
467
+ }
468
+ type Hero {
469
+ id: String
470
+ name: String
471
+ }
472
+ "# ;
473
+ let data = json ! ( {
474
+ "hero" : {
475
+ "id" : "2001" ,
476
+ "name" : "R2-D2" ,
477
+ } ,
478
+ } ) ;
479
+
480
+ let schema: RootNode < _ , _ , _ > = RootNode :: new_with_info (
481
+ data,
482
+ EmptyMutation :: new ( ) ,
483
+ EmptySubscription :: new ( ) ,
484
+ TypeInfo {
485
+ name : "Query" . to_string ( ) ,
486
+ schema : Some ( sdl. to_string ( ) ) ,
487
+ } ,
488
+ ( ) ,
489
+ ( ) ,
490
+ ) ;
491
+
492
+ let doc = r#"{
493
+ hero {
494
+ id
495
+ name
496
+ }
497
+ }"# ;
498
+ assert_eq ! (
499
+ crate :: execute( doc, None , & schema, & Variables :: new( ) , & ( ) ) . await ,
500
+ Ok ( (
501
+ graphql_value!( { "hero" : { "id" : "2001" , "name" : "R2-D2" } } ) ,
502
+ vec![ ] ,
503
+ ) ) ,
504
+ ) ;
505
+ }
425
506
}
507
+
0 commit comments