vector_api_client/gql/
health.rs

1//! Health queries/subscriptions, for asserting a GraphQL API server is alive.
2
3use graphql_client::GraphQLQuery;
4
5/// Shorthand for a Chrono datetime, set to UTC.
6type DateTime = chrono::DateTime<chrono::Utc>;
7
8/// HealthQuery is generally used to assert that the GraphQL API server is alive.
9/// The `health` field returns true.
10#[derive(GraphQLQuery, Debug, Copy, Clone)]
11#[graphql(
12    schema_path = "graphql/schema.json",
13    query_path = "graphql/queries/health.graphql",
14    response_derives = "Debug"
15)]
16pub struct HealthQuery;
17
18/// HeartbeatSubscription is a subscription that returns a 'heartbeat' in the form
19/// of a UTC timestamp. The use-case is allowing a client to assert that the server is
20/// sending regular payloads, by using the timestamp to determine when the last healthcheck
21/// was successful.
22#[derive(GraphQLQuery, Debug, Copy, Clone)]
23#[graphql(
24    schema_path = "graphql/schema.json",
25    query_path = "graphql/subscriptions/heartbeat.graphql",
26    response_derives = "Debug"
27)]
28pub struct HeartbeatSubscription;
29
30/// Extension methods for health queries.
31pub trait HealthQueryExt {
32    /// Executes a health query.
33    async fn health_query(&self) -> crate::QueryResult<HealthQuery>;
34}
35
36impl HealthQueryExt for crate::Client {
37    /// Executes a health query.
38    async fn health_query(&self) -> crate::QueryResult<HealthQuery> {
39        self.query::<HealthQuery>(&HealthQuery::build_query(health_query::Variables))
40            .await
41    }
42}
43
44/// Extension methods for health subscriptions
45pub trait HealthSubscriptionExt {
46    /// Executes a heartbeat subscription, on a millisecond `interval`.
47    fn heartbeat_subscription(
48        &self,
49        interval: i64,
50    ) -> crate::BoxedSubscription<HeartbeatSubscription>;
51}
52
53impl HealthSubscriptionExt for crate::SubscriptionClient {
54    /// Executes a heartbeat subscription, on a millisecond `interval`.
55    fn heartbeat_subscription(
56        &self,
57        interval: i64,
58    ) -> crate::BoxedSubscription<HeartbeatSubscription> {
59        let request_body =
60            HeartbeatSubscription::build_query(heartbeat_subscription::Variables { interval });
61
62        self.start::<HeartbeatSubscription>(&request_body)
63    }
64}