-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathderive_scalar.rs
More file actions
125 lines (103 loc) · 3.17 KB
/
derive_scalar.rs
File metadata and controls
125 lines (103 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use juniper::{
execute, graphql_value, EmptyMutation, EmptySubscription, FromInputValue, InputValue, RootNode,
ToInputValue, Value, Variables,
};
use crate::custom_scalar::MyScalarValue;
#[derive(Debug, PartialEq, Eq, Hash, juniper::GraphQLScalarValue)]
#[graphql(
transparent,
scalar = MyScalarValue,
specified_by_url = "https://tools.ietf.org/html/rfc4122",
)]
pub struct LargeId(i64);
#[derive(juniper::GraphQLObject)]
#[graphql(scalar = MyScalarValue)]
struct User {
id: LargeId,
}
struct Query;
#[juniper::graphql_object(scalar = MyScalarValue)]
impl Query {
fn user() -> User {
User { id: LargeId(0) }
}
}
struct Mutation;
#[juniper::graphql_object(scalar = MyScalarValue)]
impl Mutation {
fn change_user(id: LargeId) -> User {
User { id }
}
}
#[test]
fn test_scalar_value_large_id() {
let num: i64 = 4294967297;
let input_integer: InputValue<MyScalarValue> =
serde_json::from_value(serde_json::json!(num)).unwrap();
let output: LargeId =
FromInputValue::<MyScalarValue>::from_input_value(&input_integer).unwrap();
assert_eq!(output, LargeId(num));
let id = LargeId(num);
let output = ToInputValue::<MyScalarValue>::to_input_value(&id);
assert_eq!(output, InputValue::scalar(num));
}
#[tokio::test]
async fn test_scalar_value_large_specified_url() {
let schema = RootNode::<'_, _, _, _, MyScalarValue>::new_with_scalar_value(
Query,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);
let doc = r#"{
__type(name: "LargeId") {
specifiedByUrl
}
}"#;
assert_eq!(
execute(doc, None, &schema, &Variables::<MyScalarValue>::new(), &()).await,
Ok((
graphql_value!({"__type": {"specifiedByUrl": "https://tools.ietf.org/html/rfc4122"}}),
vec![],
)),
);
}
#[tokio::test]
async fn test_scalar_value_large_query() {
let schema = RootNode::<'_, _, _, _, MyScalarValue>::new_with_scalar_value(
Query,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);
let doc = r#"{
user { id }
}"#;
let val = Value::<MyScalarValue>::scalar(0_i64);
assert_eq!(
execute(doc, None, &schema, &Variables::<MyScalarValue>::new(), &()).await,
Ok((graphql_value!({"user": {"id": val}}), vec![])),
);
}
#[tokio::test]
async fn test_scalar_value_large_mutation() {
let schema = RootNode::<'_, _, _, _, MyScalarValue>::new_with_scalar_value(
Query,
Mutation,
EmptySubscription::<()>::new(),
);
let doc = r#"mutation {
changeUser(id: 1) { id }
}"#;
let val = Value::<MyScalarValue>::scalar(1_i64);
assert_eq!(
execute(doc, None, &schema, &Variables::<MyScalarValue>::new(), &()).await,
Ok((graphql_value!({"changeUser": {"id": val}}), vec![])),
);
let doc = r#"mutation {
changeUser(id: 4294967297) { id }
}"#;
let val = Value::<MyScalarValue>::scalar(4294967297_i64);
assert_eq!(
execute(doc, None, &schema, &Variables::<MyScalarValue>::new(), &()).await,
Ok((graphql_value!({"changeUser": {"id": val}}), vec![])),
);
}