armature_graphql_client/
config.rs

1//! GraphQL client configuration.
2
3use std::time::Duration;
4
5/// GraphQL client configuration.
6#[derive(Debug, Clone)]
7pub struct GraphQLClientConfig {
8    /// GraphQL endpoint URL.
9    pub endpoint: String,
10    /// WebSocket endpoint URL (for subscriptions).
11    pub ws_endpoint: Option<String>,
12    /// Request timeout.
13    pub timeout: Duration,
14    /// Default headers for all requests.
15    pub default_headers: Vec<(String, String)>,
16    /// Enable request batching.
17    pub batching: bool,
18    /// Maximum batch size.
19    pub max_batch_size: usize,
20    /// Batch delay (wait time to collect queries).
21    pub batch_delay: Duration,
22    /// Enable response caching.
23    pub caching: bool,
24    /// Cache TTL.
25    pub cache_ttl: Duration,
26    /// User agent string.
27    pub user_agent: String,
28    /// Retry failed requests.
29    pub retry_enabled: bool,
30    /// Maximum retry attempts.
31    pub max_retries: u32,
32}
33
34impl Default for GraphQLClientConfig {
35    fn default() -> Self {
36        Self {
37            endpoint: "http://localhost:4000/graphql".to_string(),
38            ws_endpoint: None,
39            timeout: Duration::from_secs(30),
40            default_headers: Vec::new(),
41            batching: false,
42            max_batch_size: 10,
43            batch_delay: Duration::from_millis(10),
44            caching: false,
45            cache_ttl: Duration::from_secs(300),
46            user_agent: format!("armature-graphql-client/{}", env!("CARGO_PKG_VERSION")),
47            retry_enabled: true,
48            max_retries: 3,
49        }
50    }
51}
52
53impl GraphQLClientConfig {
54    /// Create a new configuration builder.
55    pub fn builder() -> GraphQLClientConfigBuilder {
56        GraphQLClientConfigBuilder::default()
57    }
58
59    /// Create configuration for a specific endpoint.
60    pub fn new(endpoint: impl Into<String>) -> Self {
61        Self {
62            endpoint: endpoint.into(),
63            ..Default::default()
64        }
65    }
66}
67
68/// Builder for GraphQL client configuration.
69#[derive(Debug, Default)]
70pub struct GraphQLClientConfigBuilder {
71    config: GraphQLClientConfig,
72}
73
74impl GraphQLClientConfigBuilder {
75    /// Set the GraphQL endpoint URL.
76    pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
77        self.config.endpoint = endpoint.into();
78        self
79    }
80
81    /// Set the WebSocket endpoint for subscriptions.
82    pub fn ws_endpoint(mut self, endpoint: impl Into<String>) -> Self {
83        self.config.ws_endpoint = Some(endpoint.into());
84        self
85    }
86
87    /// Set the request timeout.
88    pub fn timeout(mut self, timeout: Duration) -> Self {
89        self.config.timeout = timeout;
90        self
91    }
92
93    /// Add a default header.
94    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
95        self.config
96            .default_headers
97            .push((name.into(), value.into()));
98        self
99    }
100
101    /// Set bearer authentication.
102    pub fn bearer_auth(mut self, token: impl Into<String>) -> Self {
103        self.config.default_headers.push((
104            "Authorization".to_string(),
105            format!("Bearer {}", token.into()),
106        ));
107        self
108    }
109
110    /// Enable request batching.
111    pub fn batching(mut self, enabled: bool) -> Self {
112        self.config.batching = enabled;
113        self
114    }
115
116    /// Set maximum batch size.
117    pub fn max_batch_size(mut self, size: usize) -> Self {
118        self.config.max_batch_size = size;
119        self
120    }
121
122    /// Set batch delay.
123    pub fn batch_delay(mut self, delay: Duration) -> Self {
124        self.config.batch_delay = delay;
125        self
126    }
127
128    /// Enable response caching.
129    pub fn caching(mut self, enabled: bool) -> Self {
130        self.config.caching = enabled;
131        self
132    }
133
134    /// Set cache TTL.
135    pub fn cache_ttl(mut self, ttl: Duration) -> Self {
136        self.config.cache_ttl = ttl;
137        self
138    }
139
140    /// Set user agent string.
141    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
142        self.config.user_agent = user_agent.into();
143        self
144    }
145
146    /// Enable or disable retry.
147    pub fn retry(mut self, enabled: bool) -> Self {
148        self.config.retry_enabled = enabled;
149        self
150    }
151
152    /// Set maximum retry attempts.
153    pub fn max_retries(mut self, retries: u32) -> Self {
154        self.config.max_retries = retries;
155        self
156    }
157
158    /// Build the configuration.
159    pub fn build(self) -> GraphQLClientConfig {
160        self.config
161    }
162}