Skip to content

Commit 74afe4c

Browse files
committed
Use graphql_value!() macro in tests asap
1 parent a3fda73 commit 74afe4c

File tree

27 files changed

+1263
-1955
lines changed

27 files changed

+1263
-1955
lines changed

examples/actix_subscriptions/src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use actix_web::{
99
};
1010

1111
use juniper::{
12-
graphql_object, graphql_subscription,
12+
graphql_object, graphql_subscription, graphql_value,
1313
tests::fixtures::starwars::schema::{Character as _, Database, Query},
14-
DefaultScalarValue, EmptyMutation, FieldError, RootNode, Value,
14+
EmptyMutation, FieldError, RootNode,
1515
};
1616
use juniper_actix::{graphql_handler, playground_handler, subscriptions::subscriptions_handler};
1717
use juniper_graphql_ws::ConnectionConfig;
@@ -77,9 +77,7 @@ impl Subscription {
7777
if counter == 2 {
7878
yield Err(FieldError::new(
7979
"some field error from handler",
80-
Value::Scalar(DefaultScalarValue::String(
81-
"some additional string".to_string(),
82-
)),
80+
graphql_value!("some additional string"),
8381
))
8482
} else {
8583
let random_id = rng.gen_range(1000..1005).to_string();

examples/warp_subscriptions/src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::{env, pin::Pin, sync::Arc, time::Duration};
44

55
use futures::{FutureExt as _, Stream};
66
use juniper::{
7-
graphql_object, graphql_subscription, DefaultScalarValue, EmptyMutation, FieldError,
8-
GraphQLEnum, RootNode, Value,
7+
graphql_object, graphql_subscription, graphql_value, EmptyMutation, FieldError, GraphQLEnum,
8+
RootNode,
99
};
1010
use juniper_graphql_ws::ConnectionConfig;
1111
use juniper_warp::{playground_filter, subscriptions::serve_graphql_ws};
@@ -117,9 +117,7 @@ impl Subscription {
117117
if counter == 2 {
118118
yield Err(FieldError::new(
119119
"some field error from handler",
120-
Value::Scalar(DefaultScalarValue::String(
121-
"some additional string".to_string(),
122-
)),
120+
graphql_value!("some additional string"),
123121
))
124122
} else {
125123
yield Ok(User {
Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::custom_scalar::MyScalarValue;
21
use juniper::{
3-
execute, EmptyMutation, EmptySubscription, FromInputValue, InputValue, RootNode, ToInputValue,
4-
Value, Variables,
2+
execute, graphql_value, EmptyMutation, EmptySubscription, FromInputValue, InputValue, RootNode,
3+
ToInputValue, Value, Variables,
54
};
65

6+
use crate::custom_scalar::MyScalarValue;
7+
78
#[derive(Debug, PartialEq, Eq, Hash, juniper::GraphQLScalarValue)]
89
#[graphql(transparent, scalar = MyScalarValue)]
910
pub struct LargeId(i64);
@@ -56,28 +57,14 @@ async fn test_scalar_value_large_query() {
5657
EmptySubscription::<()>::new(),
5758
);
5859

59-
let doc = r#"
60-
query {
61-
user { id }
62-
}"#;
60+
let doc = r#"{
61+
user { id }
62+
}"#;
6363

64+
let val = Value::<MyScalarValue>::scalar(0_i64);
6465
assert_eq!(
6566
execute(doc, None, &schema, &Variables::<MyScalarValue>::new(), &()).await,
66-
Ok((
67-
Value::object(
68-
vec![(
69-
"user",
70-
Value::object(
71-
vec![("id", Value::<MyScalarValue>::scalar(0_i64)),]
72-
.into_iter()
73-
.collect(),
74-
),
75-
)]
76-
.into_iter()
77-
.collect()
78-
),
79-
vec![]
80-
))
67+
Ok((graphql_value!({"user": {"id": val}}), vec![])),
8168
);
8269
}
8370

@@ -89,51 +76,23 @@ async fn test_scalar_value_large_mutation() {
8976
EmptySubscription::<()>::new(),
9077
);
9178

92-
let doc = r#"
93-
mutation {
94-
changeUser(id: 1) { id }
95-
}"#;
79+
let doc = r#"mutation {
80+
changeUser(id: 1) { id }
81+
}"#;
9682

83+
let val = Value::<MyScalarValue>::scalar(1_i64);
9784
assert_eq!(
9885
execute(doc, None, &schema, &Variables::<MyScalarValue>::new(), &()).await,
99-
Ok((
100-
Value::object(
101-
vec![(
102-
"changeUser",
103-
Value::object(
104-
vec![("id", Value::<MyScalarValue>::scalar(1_i64)),]
105-
.into_iter()
106-
.collect(),
107-
),
108-
)]
109-
.into_iter()
110-
.collect()
111-
),
112-
vec![]
113-
))
86+
Ok((graphql_value!({"changeUser": {"id": val}}), vec![])),
11487
);
11588

116-
let doc = r#"
117-
mutation {
118-
changeUser(id: 4294967297) { id }
119-
}"#;
89+
let doc = r#"mutation {
90+
changeUser(id: 4294967297) { id }
91+
}"#;
12092

93+
let val = Value::<MyScalarValue>::scalar(4294967297_i64);
12194
assert_eq!(
12295
execute(doc, None, &schema, &Variables::<MyScalarValue>::new(), &()).await,
123-
Ok((
124-
Value::object(
125-
vec![(
126-
"changeUser",
127-
Value::object(
128-
vec![("id", Value::<MyScalarValue>::scalar(4294967297_i64)),]
129-
.into_iter()
130-
.collect(),
131-
),
132-
)]
133-
.into_iter()
134-
.collect()
135-
),
136-
vec![]
137-
))
96+
Ok((graphql_value!({"changeUser": {"id": val}}), vec![])),
13897
);
13998
}

integration_tests/juniper_tests/src/codegen/impl_scalar.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ async fn default_name_introspection() {
222222
run_type_info_query(doc, |type_info| {
223223
assert_eq!(
224224
type_info.get_field_value("name"),
225-
Some(&Value::scalar("DefaultName"))
225+
Some(&graphql_value!("DefaultName")),
226226
);
227227
assert_eq!(
228228
type_info.get_field_value("description"),
229-
Some(&Value::null())
229+
Some(&graphql_value!(None)),
230230
);
231231
})
232232
.await;
@@ -246,11 +246,11 @@ async fn other_order_introspection() {
246246
run_type_info_query(doc, |type_info| {
247247
assert_eq!(
248248
type_info.get_field_value("name"),
249-
Some(&Value::scalar("OtherOrder"))
249+
Some(&graphql_value!("OtherOrder")),
250250
);
251251
assert_eq!(
252252
type_info.get_field_value("description"),
253-
Some(&Value::null())
253+
Some(&graphql_value!(None)),
254254
);
255255
})
256256
.await;
@@ -270,11 +270,11 @@ async fn named_introspection() {
270270
run_type_info_query(doc, |type_info| {
271271
assert_eq!(
272272
type_info.get_field_value("name"),
273-
Some(&Value::scalar("ANamedScalar"))
273+
Some(&graphql_value!("ANamedScalar")),
274274
);
275275
assert_eq!(
276276
type_info.get_field_value("description"),
277-
Some(&Value::null())
277+
Some(&graphql_value!(None)),
278278
);
279279
})
280280
.await;
@@ -294,11 +294,13 @@ async fn scalar_description_introspection() {
294294
run_type_info_query(doc, |type_info| {
295295
assert_eq!(
296296
type_info.get_field_value("name"),
297-
Some(&Value::scalar("ScalarDescription"))
297+
Some(&graphql_value!("ScalarDescription")),
298298
);
299299
assert_eq!(
300300
type_info.get_field_value("description"),
301-
Some(&Value::scalar("A sample scalar, represented as an integer"))
301+
Some(&graphql_value!(
302+
"A sample scalar, represented as an integer"
303+
)),
302304
);
303305
})
304306
.await;
@@ -318,11 +320,11 @@ async fn generated_scalar_introspection() {
318320
run_type_info_query(doc, |type_info| {
319321
assert_eq!(
320322
type_info.get_field_value("name"),
321-
Some(&Value::scalar("Generated"))
323+
Some(&graphql_value!("Generated")),
322324
);
323325
assert_eq!(
324326
type_info.get_field_value("description"),
325-
Some(&Value::null())
327+
Some(&graphql_value!(None)),
326328
);
327329
})
328330
.await;

integration_tests/juniper_tests/src/codegen/subscription_attr.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
33
use std::pin::Pin;
44

5-
use futures::{future, stream, FutureExt as _, StreamExt as _};
5+
use futures::{future, stream, FutureExt as _};
66
use juniper::{
77
execute, graphql_object, graphql_subscription, graphql_value, resolve_into_stream,
8-
DefaultScalarValue, EmptyMutation, ExecutionError, Executor, FieldError, FieldResult,
9-
GraphQLError, GraphQLInputObject, GraphQLType, IntoFieldError, RootNode, ScalarValue, Value,
10-
ValuesStream, Variables,
8+
DefaultScalarValue, EmptyMutation, Executor, FieldError, FieldResult, GraphQLInputObject,
9+
GraphQLType, IntoFieldError, RootNode, ScalarValue, Variables,
1110
};
1211

12+
use crate::util::extract_next;
13+
1314
struct Query;
1415

1516
#[graphql_object]
@@ -44,29 +45,6 @@ where
4445

4546
type Stream<'a, I> = Pin<Box<dyn futures::Stream<Item = I> + Send + 'a>>;
4647

47-
async fn extract_next<'a, S: ScalarValue>(
48-
input: Result<(Value<ValuesStream<'a, S>>, Vec<ExecutionError<S>>), GraphQLError<'a>>,
49-
) -> Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError<'a>> {
50-
let (stream, errs) = input?;
51-
if !errs.is_empty() {
52-
return Ok((Value::Null, errs));
53-
}
54-
55-
if let Value::Object(obj) = stream {
56-
for (name, mut val) in obj {
57-
if let Value::Scalar(ref mut stream) = val {
58-
return match stream.next().await {
59-
Some(Ok(val)) => Ok((graphql_value!({ name: val }), vec![])),
60-
Some(Err(e)) => Ok((Value::Null, vec![e])),
61-
None => Ok((Value::Null, vec![])),
62-
};
63-
}
64-
}
65-
}
66-
67-
panic!("Expected to get Value::Object containing a Stream")
68-
}
69-
7048
mod trivial {
7149
use super::*;
7250

integration_tests/juniper_tests/src/custom_scalar.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ async fn querying_long() {
218218
run_query("{ longField }", |result| {
219219
assert_eq!(
220220
result.get_field_value("longField"),
221-
Some(&Value::scalar(i64::from(i32::max_value()) + 1))
221+
Some(&Value::scalar(i64::from(i32::MAX) + 1))
222222
);
223223
})
224224
.await;
@@ -227,14 +227,11 @@ async fn querying_long() {
227227
#[tokio::test]
228228
async fn querying_long_arg() {
229229
run_query(
230-
&format!(
231-
"{{ longWithArg(longArg: {}) }}",
232-
i64::from(i32::max_value()) + 3
233-
),
230+
&format!("{{ longWithArg(longArg: {}) }}", i64::from(i32::MAX) + 3),
234231
|result| {
235232
assert_eq!(
236233
result.get_field_value("longWithArg"),
237-
Some(&Value::scalar(i64::from(i32::max_value()) + 3))
234+
Some(&Value::scalar(i64::from(i32::MAX) + 3))
238235
);
239236
},
240237
)
@@ -247,14 +244,14 @@ async fn querying_long_variable() {
247244
"query q($test: Long!){ longWithArg(longArg: $test) }",
248245
vec![(
249246
"test".to_owned(),
250-
InputValue::Scalar(MyScalarValue::Long(i64::from(i32::max_value()) + 42)),
247+
InputValue::Scalar(MyScalarValue::Long(i64::from(i32::MAX) + 42)),
251248
)]
252249
.into_iter()
253250
.collect(),
254251
|result| {
255252
assert_eq!(
256253
result.get_field_value("longWithArg"),
257-
Some(&Value::scalar(i64::from(i32::max_value()) + 42))
254+
Some(&Value::scalar(i64::from(i32::MAX) + 42))
258255
);
259256
},
260257
)
@@ -263,15 +260,15 @@ async fn querying_long_variable() {
263260

264261
#[test]
265262
fn deserialize_variable() {
266-
let json = format!("{{\"field\": {}}}", i64::from(i32::max_value()) + 42);
263+
let json = format!("{{\"field\": {}}}", i64::from(i32::MAX) + 42);
267264

268265
let input_value: InputValue<MyScalarValue> = serde_json::from_str(&json).unwrap();
269266
assert_eq!(
270267
input_value,
271268
InputValue::Object(vec![(
272269
Spanning::unlocated("field".into()),
273270
Spanning::unlocated(InputValue::Scalar(MyScalarValue::Long(
274-
i64::from(i32::max_value()) + 42
271+
i64::from(i32::MAX) + 42
275272
)))
276273
)])
277274
);

0 commit comments

Comments
 (0)