juniper_graphql_ws/
lib.rs1#![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#[derive(Clone, Copy, Debug)]
27pub struct ConnectionConfig<CtxT> {
28 pub context: CtxT,
30
31 pub max_in_flight_operations: usize,
36
37 pub keep_alive_interval: Duration,
43}
44
45impl<CtxT> ConnectionConfig<CtxT> {
46 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 #[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 #[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
86pub trait Init<S: ScalarValue, CtxT>: Unpin + 'static {
90 type Error: Error;
93
94 type Future: Future<Output = Result<ConnectionConfig<CtxT>, Self::Error>> + Send + 'static;
96
97 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}