|
| 1 | +use juniper::Variables; |
| 2 | +use serde::Deserialize; |
| 3 | + |
| 4 | +use crate::utils::default_for_null; |
| 5 | + |
| 6 | +/// The payload for a client's "start" message. This triggers execution of a query, mutation, or |
| 7 | +/// subscription. |
| 8 | +#[derive(Debug, Deserialize, PartialEq)] |
| 9 | +#[serde(bound(deserialize = "S: Deserialize<'de>"))] |
| 10 | +#[serde(rename_all = "camelCase")] |
| 11 | +pub struct SubscribePayload<S> { |
| 12 | + /// The document body. |
| 13 | + pub query: String, |
| 14 | + |
| 15 | + /// The optional variables. |
| 16 | + #[serde(default, deserialize_with = "default_for_null")] |
| 17 | + pub variables: Variables<S>, |
| 18 | + |
| 19 | + /// The optional operation name (required if the document contains multiple operations). |
| 20 | + pub operation_name: Option<String>, |
| 21 | + |
| 22 | + /// The optional extension data. |
| 23 | + #[serde(default, deserialize_with = "default_for_null")] |
| 24 | + pub extensions: Variables<S>, |
| 25 | +} |
| 26 | + |
| 27 | +/// ClientMessage defines the message types that clients can send. |
| 28 | +#[derive(Debug, Deserialize, PartialEq)] |
| 29 | +#[serde(bound(deserialize = "S: Deserialize<'de>"))] |
| 30 | +#[serde(rename_all = "snake_case")] |
| 31 | +#[serde(tag = "type")] |
| 32 | +pub enum ClientMessage<S> { |
| 33 | + /// ConnectionInit is sent by the client upon connecting. |
| 34 | + ConnectionInit { |
| 35 | + /// Optional parameters of any type sent from the client. These are often used for |
| 36 | + /// authentication. |
| 37 | + #[serde(default, deserialize_with = "default_for_null")] |
| 38 | + payload: Variables<S>, |
| 39 | + }, |
| 40 | + /// Ping is used for detecting failed connections, displaying latency metrics or other types of network probing. |
| 41 | + Ping { |
| 42 | + /// Optional parameters of any type used to transfer additional details about the ping. |
| 43 | + #[serde(default, deserialize_with = "default_for_null")] |
| 44 | + payload: Variables<S>, |
| 45 | + }, |
| 46 | + /// The response to the `Ping` message. |
| 47 | + Pong { |
| 48 | + /// Optional parameters of any type used to transfer additional details about the pong. |
| 49 | + #[serde(default, deserialize_with = "default_for_null")] |
| 50 | + payload: Variables<S>, |
| 51 | + }, |
| 52 | + /// Requests an operation specified in the message payload. |
| 53 | + Subscribe { |
| 54 | + /// The id of the operation. This can be anything, but must be unique. If there are other |
| 55 | + /// in-flight operations with the same id, the message will cause an error. |
| 56 | + id: String, |
| 57 | + |
| 58 | + /// The query, variables, and operation name. |
| 59 | + payload: SubscribePayload<S>, |
| 60 | + }, |
| 61 | + /// Indicates that the client has stopped listening and wants to complete the subscription. |
| 62 | + Complete { |
| 63 | + /// The id of the operation to stop. |
| 64 | + id: String, |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod test { |
| 70 | + use juniper::{graphql_vars, DefaultScalarValue}; |
| 71 | + |
| 72 | + use super::*; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test_deserialization() { |
| 76 | + type ClientMessage = super::ClientMessage<DefaultScalarValue>; |
| 77 | + |
| 78 | + assert_eq!( |
| 79 | + ClientMessage::ConnectionInit { |
| 80 | + payload: graphql_vars! {"foo": "bar"}, |
| 81 | + }, |
| 82 | + serde_json::from_str(r##"{"type": "connection_init", "payload": {"foo": "bar"}}"##) |
| 83 | + .unwrap(), |
| 84 | + ); |
| 85 | + |
| 86 | + assert_eq!( |
| 87 | + ClientMessage::ConnectionInit { |
| 88 | + payload: graphql_vars! {}, |
| 89 | + }, |
| 90 | + serde_json::from_str(r##"{"type": "connection_init"}"##).unwrap(), |
| 91 | + ); |
| 92 | + |
| 93 | + assert_eq!( |
| 94 | + ClientMessage::Subscribe { |
| 95 | + id: "foo".into(), |
| 96 | + payload: SubscribePayload { |
| 97 | + query: "query MyQuery { __typename }".into(), |
| 98 | + variables: graphql_vars! {"foo": "bar"}, |
| 99 | + operation_name: Some("MyQuery".into()), |
| 100 | + extensions: Default::default(), |
| 101 | + }, |
| 102 | + }, |
| 103 | + serde_json::from_str( |
| 104 | + r##"{"type": "subscribe", "id": "foo", "payload": { |
| 105 | + "query": "query MyQuery { __typename }", |
| 106 | + "variables": { |
| 107 | + "foo": "bar" |
| 108 | + }, |
| 109 | + "operationName": "MyQuery" |
| 110 | + }}"## |
| 111 | + ) |
| 112 | + .unwrap(), |
| 113 | + ); |
| 114 | + |
| 115 | + assert_eq!( |
| 116 | + ClientMessage::Subscribe { |
| 117 | + id: "foo".into(), |
| 118 | + payload: SubscribePayload { |
| 119 | + query: "query MyQuery { __typename }".into(), |
| 120 | + variables: graphql_vars! {}, |
| 121 | + operation_name: None, |
| 122 | + extensions: Default::default(), |
| 123 | + }, |
| 124 | + }, |
| 125 | + serde_json::from_str( |
| 126 | + r##"{"type": "subscribe", "id": "foo", "payload": { |
| 127 | + "query": "query MyQuery { __typename }" |
| 128 | + }}"## |
| 129 | + ) |
| 130 | + .unwrap(), |
| 131 | + ); |
| 132 | + |
| 133 | + assert_eq!( |
| 134 | + ClientMessage::Complete { id: "foo".into() }, |
| 135 | + serde_json::from_str(r##"{"type": "complete", "id": "foo"}"##).unwrap(), |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_deserialization_of_null() -> serde_json::Result<()> { |
| 141 | + let payload = r#"{"query":"query","variables":null}"#; |
| 142 | + let payload: SubscribePayload<DefaultScalarValue> = serde_json::from_str(payload)?; |
| 143 | + |
| 144 | + let expected = SubscribePayload { |
| 145 | + query: "query".into(), |
| 146 | + variables: graphql_vars! {}, |
| 147 | + operation_name: None, |
| 148 | + extensions: Default::default(), |
| 149 | + }; |
| 150 | + |
| 151 | + assert_eq!(expected, payload); |
| 152 | + |
| 153 | + Ok(()) |
| 154 | + } |
| 155 | +} |
0 commit comments