forked from graphql-rust/juniper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderive_input_object.rs
More file actions
168 lines (143 loc) · 4.29 KB
/
derive_input_object.rs
File metadata and controls
168 lines (143 loc) · 4.29 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use fnv::FnvHashMap;
use juniper::{
marker, DefaultScalarValue, FromInputValue, GraphQLInputObject, GraphQLType, GraphQLValue,
InputValue, ToInputValue,
};
#[derive(GraphQLInputObject, Debug, PartialEq)]
#[graphql(
name = "MyInput",
description = "input descr",
scalar = DefaultScalarValue
)]
struct Input {
regular_field: String,
#[graphql(name = "haha", default = "33", description = "haha descr")]
c: i32,
#[graphql(default)]
other: Option<bool>,
}
/// Object comment.
#[derive(GraphQLInputObject, Debug, PartialEq)]
struct DocComment {
/// Field comment.
regular_field: bool,
}
/// Doc 1.\
/// Doc 2.
///
/// Doc 4.
#[derive(GraphQLInputObject, Debug, PartialEq)]
struct MultiDocComment {
/// Field 1.
/// Field 2.
regular_field: bool,
}
/// This is not used as the description.
#[derive(GraphQLInputObject, Debug, PartialEq)]
#[graphql(description = "obj override")]
struct OverrideDocComment {
/// This is not used as the description.
#[graphql(description = "field override")]
regular_field: bool,
}
#[derive(Debug, PartialEq)]
struct Fake;
impl<'a> marker::IsInputType for &'a Fake {}
impl<'a> FromInputValue for &'a Fake {
fn from_input_value(_v: &InputValue) -> Option<&'a Fake> {
None
}
}
impl<'a> ToInputValue for &'a Fake {
fn to_input_value(&self) -> InputValue {
InputValue::scalar("this is fake")
}
}
impl<'a> GraphQLType for &'a Fake {
fn name(_: &()) -> Option<&'static str> {
None
}
fn meta<'r>(_: &(), registry: &mut juniper::Registry<'r>) -> juniper::meta::MetaType<'r> {
let meta = registry.build_enum_type::<&'a Fake>(
&(),
&[juniper::meta::EnumValue {
name: "fake".to_string(),
description: None,
deprecation_status: juniper::meta::DeprecationStatus::Current,
}],
);
meta.into_meta()
}
}
impl<'a> GraphQLValue for &'a Fake {
type Context = ();
type TypeInfo = ();
fn type_name<'i>(&self, info: &'i Self::TypeInfo) -> Option<&'i str> {
<Self as GraphQLType>::name(info)
}
}
#[derive(GraphQLInputObject, Debug, PartialEq)]
#[graphql(scalar = DefaultScalarValue)]
struct WithLifetime<'a> {
regular_field: &'a Fake,
}
#[test]
fn test_derived_input_object() {
assert_eq!(<Input as GraphQLType>::name(&()), Some("MyInput"));
// Validate meta info.
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = Input::meta(&(), &mut registry);
assert_eq!(meta.name(), Some("MyInput"));
assert_eq!(meta.description(), Some(&"input descr".to_string()));
// Test default value injection.
let input_no_defaults: InputValue = ::serde_json::from_value(serde_json::json!({
"regularField": "a",
}))
.unwrap();
let output_no_defaults: Input = FromInputValue::from_input_value(&input_no_defaults).unwrap();
assert_eq!(
output_no_defaults,
Input {
regular_field: "a".into(),
c: 33,
other: None,
}
);
// Test with all values supplied.
let input: InputValue = ::serde_json::from_value(serde_json::json!({
"regularField": "a",
"haha": 55,
"other": true,
}))
.unwrap();
let output: Input = FromInputValue::from_input_value(&input).unwrap();
assert_eq!(
output,
Input {
regular_field: "a".into(),
c: 55,
other: Some(true),
}
);
}
#[test]
fn test_doc_comment() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = DocComment::meta(&(), &mut registry);
assert_eq!(meta.description(), Some(&"Object comment.".to_string()));
}
#[test]
fn test_multi_doc_comment() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = MultiDocComment::meta(&(), &mut registry);
assert_eq!(
meta.description(),
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
);
}
#[test]
fn test_doc_comment_override() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = OverrideDocComment::meta(&(), &mut registry);
assert_eq!(meta.description(), Some(&"obj override".to_string()));
}