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
//! Health queries/subscriptions, for asserting a GraphQL API server is alive.

use graphql_client::GraphQLQuery;

/// Shorthand for a Chrono datetime, set to UTC.
type DateTime = chrono::DateTime<chrono::Utc>;

/// HealthQuery is generally used to assert that the GraphQL API server is alive.
/// The `health` field returns true.
#[derive(GraphQLQuery, Debug, Copy, Clone)]
#[graphql(
    schema_path = "graphql/schema.json",
    query_path = "graphql/queries/health.graphql",
    response_derives = "Debug"
)]
pub struct HealthQuery;

/// HeartbeatSubscription is a subscription that returns a 'heartbeat' in the form
/// of a UTC timestamp. The use-case is allowing a client to assert that the server is
/// sending regular payloads, by using the timestamp to determine when the last healthcheck
/// was successful.
#[derive(GraphQLQuery, Debug, Copy, Clone)]
#[graphql(
    schema_path = "graphql/schema.json",
    query_path = "graphql/subscriptions/heartbeat.graphql",
    response_derives = "Debug"
)]
pub struct HeartbeatSubscription;

/// Extension methods for health queries.
pub trait HealthQueryExt {
    /// Executes a health query.
    async fn health_query(&self) -> crate::QueryResult<HealthQuery>;
}

impl HealthQueryExt for crate::Client {
    /// Executes a health query.
    async fn health_query(&self) -> crate::QueryResult<HealthQuery> {
        self.query::<HealthQuery>(&HealthQuery::build_query(health_query::Variables))
            .await
    }
}

/// Extension methods for health subscriptions
pub trait HealthSubscriptionExt {
    /// Executes a heartbeat subscription, on a millisecond `interval`.
    fn heartbeat_subscription(
        &self,
        interval: i64,
    ) -> crate::BoxedSubscription<HeartbeatSubscription>;
}

impl HealthSubscriptionExt for crate::SubscriptionClient {
    /// Executes a heartbeat subscription, on a millisecond `interval`.
    fn heartbeat_subscription(
        &self,
        interval: i64,
    ) -> crate::BoxedSubscription<HeartbeatSubscription> {
        let request_body =
            HeartbeatSubscription::build_query(heartbeat_subscription::Variables { interval });

        self.start::<HeartbeatSubscription>(&request_body)
    }
}