Skip to content

Commit

Permalink
feat: add support for legacy websocket events
Browse files Browse the repository at this point in the history
feat: add support for legacy websocket events

feat: add support for legacy graphql events, fix tests

revert: remove payload on client-build

revert: remove payload on client-build

chore: cleanup
  • Loading branch information
logandeancall committed Jan 30, 2025
1 parent 9e8ee6a commit b51c405
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/next/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ impl ConnectionActor {
};

match event {
event @ (Event::Next { .. } | Event::Error { .. }) => {
event @ (Event::Next { .. }
| Event::Data { .. }
| Event::Error { .. }
| Event::ConnectionError { .. }) => {
let Some(id) = event.id().unwrap().parse::<usize>().ok() else {
return Some(Message::close(Reason::UnknownSubscription));
};
Expand Down Expand Up @@ -151,6 +154,8 @@ impl ConnectionActor {
Event::ConnectionAck { .. } => Some(Message::close(Reason::UnexpectedAck)),
Event::Ping { .. } => Some(Message::graphql_pong()),
Event::Pong { .. } => None,
Event::ConnectionKeepAlive { .. } => None,
Event::KeepAlive { .. } => None,
}
}

Expand Down Expand Up @@ -236,6 +241,7 @@ impl Message {
impl Event {
fn forwarding_payload(self) -> Option<Value> {
match self {
Event::Data { payload, .. } => payload,
Event::Next { payload, .. } => Some(payload),
Event::Error { payload, .. } => Some(json!({"errors": payload})),
_ => None,
Expand Down
3 changes: 3 additions & 0 deletions src/next/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ impl ClientBuilder {
trace!("connection_ack received, handshake completed");
break;
}
Event::KeepAlive { .. } => {
continue;
}
event => {
connection
.send(Message::Close {
Expand Down
27 changes: 24 additions & 3 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ pub enum Event {
id: String,
payload: Vec<serde_json::Value>,
},
#[serde(rename = "connection_error")]
ConnectionError {
id: String,
payload: Vec<serde_json::Value>,
},
#[serde(rename = "complete")]
Complete { id: String },
#[serde(rename = "connection_ack")]
Expand All @@ -67,26 +72,42 @@ pub enum Event {
Ping { payload: Option<serde_json::Value> },
#[serde(rename = "pong")]
Pong { payload: Option<serde_json::Value> },
#[serde(rename = "ka")]
KeepAlive { payload: Option<serde_json::Value> },
#[serde(rename = "connection_keep_alive")]
ConnectionKeepAlive { payload: Option<serde_json::Value> },
#[serde(rename = "data")]
Data {
id: String,
payload: Option<serde_json::Value>,
},
}

impl Event {
pub fn id(&self) -> Option<&str> {
match self {
Event::Next { id, .. } | Event::Complete { id, .. } | Event::Error { id, .. } => {
Some(id.as_ref())
}
Event::Next { id, .. }
| Event::Data { id, .. }
| Event::Complete { id, .. }
| Event::Error { id, .. }
| Event::ConnectionError { id, .. } => Some(id.as_ref()),
Event::Ping { .. } | Event::Pong { .. } | Event::ConnectionAck { .. } => None,
Event::KeepAlive { .. } | Event::ConnectionKeepAlive { .. } => None,
}
}

pub fn r#type(&self) -> &'static str {
match self {
Event::Next { .. } => "next",
Event::Data { .. } => "data",
Event::Complete { .. } => "complete",
Event::Error { .. } => "error",
Event::ConnectionError { .. } => "connection_error",
Event::Ping { .. } => "ping",
Event::Pong { .. } => "pong",
Event::ConnectionAck { .. } => "connection_ack",
Event::KeepAlive { .. } => "ka",
Event::ConnectionKeepAlive { .. } => "connection_keep_alive",
}
}
}

0 comments on commit b51c405

Please sign in to comment.