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
use juniper::{ScalarValue, Variables};
use serde::Deserialize;
use crate::utils::default_for_null;
#[derive(Debug, Deserialize, PartialEq)]
#[serde(bound(deserialize = "S: ScalarValue"))]
#[serde(rename_all = "camelCase")]
pub struct StartPayload<S: ScalarValue> {
pub query: String,
#[serde(default, deserialize_with = "default_for_null")]
pub variables: Variables<S>,
pub operation_name: Option<String>,
}
#[derive(Debug, Deserialize, PartialEq)]
#[serde(bound(deserialize = "S: ScalarValue"))]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum ClientMessage<S: ScalarValue> {
ConnectionInit {
#[serde(default, deserialize_with = "default_for_null")]
payload: Variables<S>,
},
Start {
id: String,
payload: StartPayload<S>,
},
Stop {
id: String,
},
ConnectionTerminate,
}
#[cfg(test)]
mod test {
use super::*;
use juniper::{DefaultScalarValue, InputValue};
#[test]
fn test_deserialization() {
type ClientMessage = super::ClientMessage<DefaultScalarValue>;
assert_eq!(
ClientMessage::ConnectionInit {
payload: [("foo".to_string(), InputValue::scalar("bar"))]
.iter()
.cloned()
.collect(),
},
serde_json::from_str(r##"{"type": "connection_init", "payload": {"foo": "bar"}}"##)
.unwrap(),
);
assert_eq!(
ClientMessage::ConnectionInit {
payload: Variables::default(),
},
serde_json::from_str(r##"{"type": "connection_init"}"##).unwrap(),
);
assert_eq!(
ClientMessage::Start {
id: "foo".to_string(),
payload: StartPayload {
query: "query MyQuery { __typename }".to_string(),
variables: [("foo".to_string(), InputValue::scalar("bar"))]
.iter()
.cloned()
.collect(),
operation_name: Some("MyQuery".to_string()),
},
},
serde_json::from_str(
r##"{"type": "start", "id": "foo", "payload": {
"query": "query MyQuery { __typename }",
"variables": {
"foo": "bar"
},
"operationName": "MyQuery"
}}"##
)
.unwrap(),
);
assert_eq!(
ClientMessage::Start {
id: "foo".to_string(),
payload: StartPayload {
query: "query MyQuery { __typename }".to_string(),
variables: Variables::default(),
operation_name: None,
},
},
serde_json::from_str(
r##"{"type": "start", "id": "foo", "payload": {
"query": "query MyQuery { __typename }"
}}"##
)
.unwrap(),
);
assert_eq!(
ClientMessage::Stop {
id: "foo".to_string()
},
serde_json::from_str(r##"{"type": "stop", "id": "foo"}"##).unwrap(),
);
assert_eq!(
ClientMessage::ConnectionTerminate,
serde_json::from_str(r##"{"type": "connection_terminate"}"##).unwrap(),
);
}
#[test]
fn test_deserialization_of_null() -> serde_json::Result<()> {
let payload = r#"{"query":"query","variables":null}"#;
let payload: StartPayload<DefaultScalarValue> = serde_json::from_str(payload)?;
let expected = StartPayload {
query: "query".into(),
variables: Variables::default(),
operation_name: None,
};
assert_eq!(expected, payload);
Ok(())
}
}