|
| 1 | +use juniper::{ |
| 2 | + graphql_object, graphql_value, EmptyMutation, EmptySubscription, GraphQLInputObject, RootNode, |
| 3 | + Variables, |
| 4 | +}; |
| 5 | + |
| 6 | +mod as_output_field { |
| 7 | + use super::*; |
| 8 | + |
| 9 | + struct Query; |
| 10 | + |
| 11 | + #[graphql_object] |
| 12 | + impl Query { |
| 13 | + fn roll() -> [bool; 3] { |
| 14 | + [true, false, true] |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + #[tokio::test] |
| 19 | + async fn works() { |
| 20 | + let query = r#" |
| 21 | + query Query { |
| 22 | + roll |
| 23 | + } |
| 24 | + "#; |
| 25 | + |
| 26 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 27 | + let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) |
| 28 | + .await |
| 29 | + .unwrap(); |
| 30 | + |
| 31 | + assert_eq!(errors.len(), 0); |
| 32 | + assert_eq!(res, graphql_value!({"roll": [true, false, true]})); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +mod as_input_field { |
| 37 | + use super::*; |
| 38 | + |
| 39 | + #[derive(GraphQLInputObject)] |
| 40 | + struct Input { |
| 41 | + two: [bool; 2], |
| 42 | + } |
| 43 | + |
| 44 | + #[derive(GraphQLInputObject)] |
| 45 | + struct InputSingle { |
| 46 | + one: [bool; 1], |
| 47 | + } |
| 48 | + |
| 49 | + struct Query; |
| 50 | + |
| 51 | + #[graphql_object] |
| 52 | + impl Query { |
| 53 | + fn first(input: InputSingle) -> bool { |
| 54 | + input.one[0] |
| 55 | + } |
| 56 | + |
| 57 | + fn second(input: Input) -> bool { |
| 58 | + input.two[1] |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + #[tokio::test] |
| 63 | + async fn works() { |
| 64 | + let query = r#" |
| 65 | + query Query { |
| 66 | + second(input: { two: [true, false] }) |
| 67 | + } |
| 68 | + "#; |
| 69 | + |
| 70 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 71 | + let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) |
| 72 | + .await |
| 73 | + .unwrap(); |
| 74 | + |
| 75 | + assert_eq!(errors.len(), 0); |
| 76 | + assert_eq!(res, graphql_value!({"second": false})); |
| 77 | + } |
| 78 | + |
| 79 | + #[tokio::test] |
| 80 | + async fn fails_on_incorrect_count() { |
| 81 | + let query = r#" |
| 82 | + query Query { |
| 83 | + second(input: { two: [true, true, false] }) |
| 84 | + } |
| 85 | + "#; |
| 86 | + |
| 87 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 88 | + let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; |
| 89 | + |
| 90 | + assert!(res.is_err()); |
| 91 | + assert!(res |
| 92 | + .unwrap_err() |
| 93 | + .to_string() |
| 94 | + .contains(r#"Invalid value for argument "input", expected type "Input!""#)); |
| 95 | + } |
| 96 | + |
| 97 | + #[tokio::test] |
| 98 | + async fn cannot_coerce_from_raw_value_if_multiple() { |
| 99 | + let query = r#" |
| 100 | + query Query { |
| 101 | + second(input: { two: true }) |
| 102 | + } |
| 103 | + "#; |
| 104 | + |
| 105 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 106 | + let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; |
| 107 | + |
| 108 | + assert!(res.is_err()); |
| 109 | + assert!(res |
| 110 | + .unwrap_err() |
| 111 | + .to_string() |
| 112 | + .contains(r#"Invalid value for argument "input", expected type "Input!""#)); |
| 113 | + } |
| 114 | + |
| 115 | + #[tokio::test] |
| 116 | + async fn can_coerce_from_raw_value_if_single() { |
| 117 | + let query = r#" |
| 118 | + query Query { |
| 119 | + first(input: { one: true }) |
| 120 | + } |
| 121 | + "#; |
| 122 | + |
| 123 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 124 | + let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) |
| 125 | + .await |
| 126 | + .unwrap(); |
| 127 | + |
| 128 | + assert_eq!(errors.len(), 0); |
| 129 | + assert_eq!(res, graphql_value!({"first": true})); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +mod as_input_argument { |
| 134 | + use super::*; |
| 135 | + |
| 136 | + struct Query; |
| 137 | + |
| 138 | + #[graphql_object] |
| 139 | + impl Query { |
| 140 | + fn second(input: [bool; 2]) -> bool { |
| 141 | + input[1] |
| 142 | + } |
| 143 | + |
| 144 | + fn first(input: [bool; 1]) -> bool { |
| 145 | + input[0] |
| 146 | + } |
| 147 | + |
| 148 | + #[graphql(arguments(input(default = [true, false, false])))] |
| 149 | + fn third(input: [bool; 3]) -> bool { |
| 150 | + input[2] |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + #[tokio::test] |
| 155 | + async fn works() { |
| 156 | + let query = r#" |
| 157 | + query Query { |
| 158 | + second(input: [false, true]) |
| 159 | + } |
| 160 | + "#; |
| 161 | + |
| 162 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 163 | + let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) |
| 164 | + .await |
| 165 | + .unwrap(); |
| 166 | + |
| 167 | + assert_eq!(errors.len(), 0); |
| 168 | + assert_eq!(res, graphql_value!({"second": true})); |
| 169 | + } |
| 170 | + |
| 171 | + #[tokio::test] |
| 172 | + async fn fails_on_incorrect_count() { |
| 173 | + let query = r#" |
| 174 | + query Query { |
| 175 | + second(input: [true, true, false]) |
| 176 | + } |
| 177 | + "#; |
| 178 | + |
| 179 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 180 | + let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; |
| 181 | + |
| 182 | + assert!(res.is_err()); |
| 183 | + assert!(res |
| 184 | + .unwrap_err() |
| 185 | + .to_string() |
| 186 | + .contains(r#"Invalid value for argument "input", expected type "[Boolean!]!""#)); |
| 187 | + } |
| 188 | + |
| 189 | + #[tokio::test] |
| 190 | + async fn cannot_coerce_from_raw_value_if_multiple() { |
| 191 | + let query = r#" |
| 192 | + query Query { |
| 193 | + second(input: true) |
| 194 | + } |
| 195 | + "#; |
| 196 | + |
| 197 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 198 | + let res = juniper::execute(query, None, &schema, &Variables::new(), &()).await; |
| 199 | + |
| 200 | + assert!(res.is_err()); |
| 201 | + assert!(res |
| 202 | + .unwrap_err() |
| 203 | + .to_string() |
| 204 | + .contains(r#"Invalid value for argument "input", expected type "[Boolean!]!""#)); |
| 205 | + } |
| 206 | + |
| 207 | + #[tokio::test] |
| 208 | + async fn can_coerce_from_raw_value_if_single() { |
| 209 | + let query = r#" |
| 210 | + query Query { |
| 211 | + first(input: true) |
| 212 | + } |
| 213 | + "#; |
| 214 | + |
| 215 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 216 | + let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) |
| 217 | + .await |
| 218 | + .unwrap(); |
| 219 | + |
| 220 | + assert_eq!(errors.len(), 0); |
| 221 | + assert_eq!(res, graphql_value!({"first": true})); |
| 222 | + } |
| 223 | + |
| 224 | + #[tokio::test] |
| 225 | + async fn picks_default() { |
| 226 | + let query = r#" |
| 227 | + query Query { |
| 228 | + third |
| 229 | + } |
| 230 | + "#; |
| 231 | + |
| 232 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 233 | + let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) |
| 234 | + .await |
| 235 | + .unwrap(); |
| 236 | + |
| 237 | + assert_eq!(errors.len(), 0); |
| 238 | + assert_eq!(res, graphql_value!({"third": false})); |
| 239 | + } |
| 240 | + |
| 241 | + #[tokio::test] |
| 242 | + async fn picks_specified_over_default() { |
| 243 | + let query = r#" |
| 244 | + query Query { |
| 245 | + third(input: [false, false, true]) |
| 246 | + } |
| 247 | + "#; |
| 248 | + |
| 249 | + let schema = RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()); |
| 250 | + let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &()) |
| 251 | + .await |
| 252 | + .unwrap(); |
| 253 | + |
| 254 | + assert_eq!(errors.len(), 0); |
| 255 | + assert_eq!(res, graphql_value!({"third": true})); |
| 256 | + } |
| 257 | +} |
0 commit comments