juniper_graphql_ws/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![cfg_attr(any(doc, test), doc = include_str!("../README.md"))]
3#![cfg_attr(not(any(doc, test)), doc = env!("CARGO_PKG_NAME"))]
4
5#[cfg(not(any(feature = "graphql-transport-ws", feature = "graphql-ws")))]
6compile_error!(
7    r#"at least one feature must be enabled (either "graphql-transport-ws" or "graphql-ws")"#
8);
9
10#[cfg(feature = "graphql-transport-ws")]
11pub mod graphql_transport_ws;
12#[cfg(feature = "graphql-ws")]
13pub mod graphql_ws;
14mod schema;
15mod server_message;
16mod util;
17
18use std::{convert::Infallible, error::Error, future, time::Duration};
19
20use juniper::{ScalarValue, Variables};
21
22pub use self::schema::{ArcSchema, Schema};
23
24/// ConnectionConfig is used to configure the connection once the client sends the ConnectionInit
25/// message.
26#[derive(Clone, Copy, Debug)]
27pub struct ConnectionConfig<CtxT> {
28    /// Custom-provided [`juniper::Context`].
29    pub context: CtxT,
30
31    /// Maximum number of in-flight operations that a connection can have.
32    ///
33    /// If this number is exceeded, attempting to start more will result in an error.
34    /// By default, there is no limit to in-flight operations.
35    pub max_in_flight_operations: usize,
36
37    /// Interval at which to send keep-alives.
38    ///
39    /// Specifying a [`Duration::ZERO`] will disable keep-alives.
40    ///
41    /// By default, keep-alives are sent every 15 seconds.
42    pub keep_alive_interval: Duration,
43}
44
45impl<CtxT> ConnectionConfig<CtxT> {
46    /// Constructs the configuration required for a connection to be accepted.
47    pub fn new(context: CtxT) -> Self {
48        Self {
49            context,
50            max_in_flight_operations: 0,
51            keep_alive_interval: Duration::from_secs(15),
52        }
53    }
54
55    /// Specifies the maximum number of in-flight operations that a connection can have.
56    ///
57    /// If this number is exceeded, attempting to start more will result in an error.
58    /// By default, there is no limit to in-flight operations.
59    #[must_use]
60    pub fn with_max_in_flight_operations(mut self, max: usize) -> Self {
61        self.max_in_flight_operations = max;
62        self
63    }
64
65    /// Specifies the interval at which to send keep-alives.
66    ///
67    /// Specifying a [`Duration::ZERO`] will disable keep-alives.
68    ///
69    /// By default, keep-alives are sent every 15 seconds.
70    #[must_use]
71    pub fn with_keep_alive_interval(mut self, interval: Duration) -> Self {
72        self.keep_alive_interval = interval;
73        self
74    }
75}
76
77impl<S: ScalarValue, CtxT: Unpin + Send + 'static> Init<S, CtxT> for ConnectionConfig<CtxT> {
78    type Error = Infallible;
79    type Future = future::Ready<Result<Self, Self::Error>>;
80
81    fn init(self, _params: Variables<S>) -> Self::Future {
82        future::ready(Ok(self))
83    }
84}
85
86/// Init defines the requirements for types that can provide connection configurations when
87/// ConnectionInit messages are received. Implementations are provided for `ConnectionConfig` and
88/// closures that meet the requirements.
89pub trait Init<S: ScalarValue, CtxT>: Unpin + 'static {
90    /// The error that is returned on failure. The formatted error will be used as the contents of
91    /// the "message" field sent back to the client.
92    type Error: Error;
93
94    /// The future configuration type.
95    type Future: Future<Output = Result<ConnectionConfig<CtxT>, Self::Error>> + Send + 'static;
96
97    /// Returns a future for the configuration to use.
98    fn init(self, params: Variables<S>) -> Self::Future;
99}
100
101impl<F, S, CtxT, Fut, E> Init<S, CtxT> for F
102where
103    S: ScalarValue,
104    F: FnOnce(Variables<S>) -> Fut + Unpin + 'static,
105    Fut: Future<Output = Result<ConnectionConfig<CtxT>, E>> + Send + 'static,
106    E: Error,
107{
108    type Error = E;
109    type Future = Fut;
110
111    fn init(self, params: Variables<S>) -> Fut {
112        self(params)
113    }
114}