Skip to main content

fraiseql_core/schema/compiled/
argument.rs

1use serde::{Deserialize, Serialize};
2
3use crate::schema::{
4    field_type::{DeprecationInfo, FieldType},
5    graphql_value::GraphQLValue,
6};
7
8/// Query/mutation/subscription argument definition.
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10pub struct ArgumentDefinition {
11    /// Argument name.
12    pub name: String,
13
14    /// Argument type.
15    pub arg_type: FieldType,
16
17    /// Is this argument optional?
18    #[serde(default)]
19    pub nullable: bool,
20
21    /// Default value.
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    pub default_value: Option<GraphQLValue>,
24
25    /// Description.
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub description: Option<String>,
28
29    /// Deprecation information (from @deprecated directive).
30    /// When set, this argument is marked as deprecated in the schema.
31    /// Per GraphQL spec, deprecated arguments should still be accepted but
32    /// clients are encouraged to migrate to alternatives.
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    pub deprecation: Option<DeprecationInfo>,
35}
36
37impl ArgumentDefinition {
38    /// Create a new required argument.
39    #[must_use]
40    pub fn new(name: impl Into<String>, arg_type: FieldType) -> Self {
41        Self {
42            name: name.into(),
43            arg_type,
44            nullable: false,
45            default_value: None,
46            description: None,
47            deprecation: None,
48        }
49    }
50
51    /// Create a new optional argument.
52    #[must_use]
53    pub fn optional(name: impl Into<String>, arg_type: FieldType) -> Self {
54        Self {
55            name: name.into(),
56            arg_type,
57            nullable: true,
58            default_value: None,
59            description: None,
60            deprecation: None,
61        }
62    }
63
64    /// Mark this argument as deprecated.
65    ///
66    /// # Example
67    ///
68    /// ```
69    /// use fraiseql_core::schema::{ArgumentDefinition, FieldType};
70    ///
71    /// let arg = ArgumentDefinition::optional("oldLimit", FieldType::Int)
72    ///     .deprecated(Some("Use 'first' instead".to_string()));
73    /// assert!(arg.is_deprecated());
74    /// ```
75    #[must_use]
76    pub fn deprecated(mut self, reason: Option<String>) -> Self {
77        self.deprecation = Some(DeprecationInfo { reason });
78        self
79    }
80
81    /// Check if this argument is deprecated.
82    #[must_use]
83    pub const fn is_deprecated(&self) -> bool {
84        self.deprecation.is_some()
85    }
86
87    /// Get the deprecation reason if deprecated.
88    #[must_use]
89    pub fn deprecation_reason(&self) -> Option<&str> {
90        self.deprecation.as_ref().and_then(|d| d.reason.as_deref())
91    }
92}
93
94/// Auto-wired query parameters.
95///
96/// These are standard parameters automatically added to list queries.
97#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
98#[allow(clippy::struct_excessive_bools)] // Reason: these are intentional feature flags
99pub struct AutoParams {
100    /// Enable `where` filtering.
101    #[serde(default)]
102    pub has_where: bool,
103
104    /// Enable `orderBy` sorting.
105    #[serde(default)]
106    pub has_order_by: bool,
107
108    /// Enable `limit` pagination.
109    #[serde(default)]
110    pub has_limit: bool,
111
112    /// Enable `offset` pagination.
113    #[serde(default)]
114    pub has_offset: bool,
115}
116
117impl AutoParams {
118    /// Create with all auto-params enabled (common for list queries).
119    #[must_use]
120    pub const fn all() -> Self {
121        Self {
122            has_where:    true,
123            has_order_by: true,
124            has_limit:    true,
125            has_offset:   true,
126        }
127    }
128
129    /// Create with no auto-params (common for single-item queries).
130    #[must_use]
131    pub fn none() -> Self {
132        Self::default()
133    }
134}