|
| 1 | +use juniper::{ScalarValue, Variables}; |
| 2 | + |
| 3 | +/// The payload for a client's "start" message. This triggers execution of a query, mutation, or |
| 4 | +/// subscription. |
| 5 | +#[derive(Debug, Deserialize, PartialEq)] |
| 6 | +#[serde(bound(deserialize = "S: ScalarValue"))] |
| 7 | +#[serde(rename_all = "camelCase")] |
| 8 | +pub struct StartPayload<S: ScalarValue> { |
| 9 | + /// The document body. |
| 10 | + pub query: String, |
| 11 | + |
| 12 | + /// The optional variables. |
| 13 | + #[serde(default)] |
| 14 | + pub variables: Variables<S>, |
| 15 | + |
| 16 | + /// The optional operation name (required if the document contains multiple operations). |
| 17 | + pub operation_name: Option<String>, |
| 18 | +} |
| 19 | + |
| 20 | +/// ClientMessage defines the message types that clients can send. |
| 21 | +#[derive(Debug, Deserialize, PartialEq)] |
| 22 | +#[serde(bound(deserialize = "S: ScalarValue"))] |
| 23 | +#[serde(rename_all = "snake_case")] |
| 24 | +#[serde(tag = "type")] |
| 25 | +pub enum ClientMessage<S: ScalarValue> { |
| 26 | + /// ConnectionInit is sent by the client upon connecting. |
| 27 | + ConnectionInit { |
| 28 | + /// Optional parameters of any type sent from the client. These are often used for |
| 29 | + /// authentication. |
| 30 | + #[serde(default)] |
| 31 | + payload: Variables<S>, |
| 32 | + }, |
| 33 | + /// Start messages are used to execute a GraphQL operation. |
| 34 | + Start { |
| 35 | + /// The id of the operation. This can be anything, but must be unique. If there are other |
| 36 | + /// in-flight operations with the same id, the message will be ignored or cause an error. |
| 37 | + id: String, |
| 38 | + |
| 39 | + /// The query, variables, and operation name. |
| 40 | + payload: StartPayload<S>, |
| 41 | + }, |
| 42 | + /// Stop messages are used to unsubscribe from a subscription. |
| 43 | + Stop { |
| 44 | + /// The id of the operation to stop. |
| 45 | + id: String, |
| 46 | + }, |
| 47 | + /// ConnectionTerminate is used to terminate the connection. |
| 48 | + ConnectionTerminate, |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod test { |
| 53 | + use super::*; |
| 54 | + use juniper::{DefaultScalarValue, InputValue}; |
| 55 | + |
| 56 | + #[test] |
| 57 | + fn test_deserialization() { |
| 58 | + type ClientMessage = super::ClientMessage<DefaultScalarValue>; |
| 59 | + |
| 60 | + assert_eq!( |
| 61 | + ClientMessage::ConnectionInit { |
| 62 | + payload: [("foo".to_string(), InputValue::scalar("bar"))] |
| 63 | + .iter() |
| 64 | + .cloned() |
| 65 | + .collect(), |
| 66 | + }, |
| 67 | + serde_json::from_str(r##"{"type": "connection_init", "payload": {"foo": "bar"}}"##) |
| 68 | + .unwrap(), |
| 69 | + ); |
| 70 | + |
| 71 | + assert_eq!( |
| 72 | + ClientMessage::ConnectionInit { |
| 73 | + payload: Variables::default(), |
| 74 | + }, |
| 75 | + serde_json::from_str(r##"{"type": "connection_init"}"##).unwrap(), |
| 76 | + ); |
| 77 | + |
| 78 | + assert_eq!( |
| 79 | + ClientMessage::Start { |
| 80 | + id: "foo".to_string(), |
| 81 | + payload: StartPayload { |
| 82 | + query: "query MyQuery { __typename }".to_string(), |
| 83 | + variables: [("foo".to_string(), InputValue::scalar("bar"))] |
| 84 | + .iter() |
| 85 | + .cloned() |
| 86 | + .collect(), |
| 87 | + operation_name: Some("MyQuery".to_string()), |
| 88 | + }, |
| 89 | + }, |
| 90 | + serde_json::from_str( |
| 91 | + r##"{"type": "start", "id": "foo", "payload": { |
| 92 | + "query": "query MyQuery { __typename }", |
| 93 | + "variables": { |
| 94 | + "foo": "bar" |
| 95 | + }, |
| 96 | + "operationName": "MyQuery" |
| 97 | + }}"## |
| 98 | + ) |
| 99 | + .unwrap(), |
| 100 | + ); |
| 101 | + |
| 102 | + assert_eq!( |
| 103 | + ClientMessage::Start { |
| 104 | + id: "foo".to_string(), |
| 105 | + payload: StartPayload { |
| 106 | + query: "query MyQuery { __typename }".to_string(), |
| 107 | + variables: Variables::default(), |
| 108 | + operation_name: None, |
| 109 | + }, |
| 110 | + }, |
| 111 | + serde_json::from_str( |
| 112 | + r##"{"type": "start", "id": "foo", "payload": { |
| 113 | + "query": "query MyQuery { __typename }" |
| 114 | + }}"## |
| 115 | + ) |
| 116 | + .unwrap(), |
| 117 | + ); |
| 118 | + |
| 119 | + assert_eq!( |
| 120 | + ClientMessage::Stop { |
| 121 | + id: "foo".to_string() |
| 122 | + }, |
| 123 | + serde_json::from_str(r##"{"type": "stop", "id": "foo"}"##).unwrap(), |
| 124 | + ); |
| 125 | + |
| 126 | + assert_eq!( |
| 127 | + ClientMessage::ConnectionTerminate, |
| 128 | + serde_json::from_str(r##"{"type": "connection_terminate"}"##).unwrap(), |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments