Skip to main content

fraiseql_cli/schema/intermediate/
types.rs

1//! Core type structs: `IntermediateType`, `IntermediateField`, `IntermediateEnum`,
2//! `IntermediateEnumValue`, `IntermediateScalar`, `IntermediateDeprecation`.
3
4use fraiseql_core::validation::ValidationRule;
5use serde::{Deserialize, Serialize};
6
7use super::fragments::IntermediateAppliedDirective;
8
9/// Type definition in intermediate format
10#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
11pub struct IntermediateType {
12    /// Type name (e.g., "User")
13    pub name: String,
14
15    /// Type fields
16    pub fields: Vec<IntermediateField>,
17
18    /// Type description (from docstring)
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub description: Option<String>,
21
22    /// Interfaces this type implements (GraphQL spec §3.6)
23    #[serde(default, skip_serializing_if = "Vec::is_empty")]
24    pub implements: Vec<String>,
25
26    /// Role required to see this type in introspection and access queries returning it.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub requires_role: Option<String>,
29
30    /// Whether this type is a mutation error type (tagged with `@fraiseql.error`).
31    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
32    pub is_error: bool,
33
34    /// Whether this type implements the Relay Node interface.
35    /// When true, the compiler generates global node IDs (`base64("TypeName:uuid")`)
36    /// and validates that `pk_{entity}` (BIGINT) is present in the view's data JSONB.
37    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
38    pub relay: bool,
39}
40
41/// Field definition in intermediate format
42///
43/// **NOTE**: Uses `type` field (not `field_type`)
44/// This is the language-agnostic format. Rust conversion happens in converter.
45#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
46pub struct IntermediateField {
47    /// Field name (e.g., "id")
48    pub name: String,
49
50    /// Field type name (e.g., "Int", "String", "User")
51    ///
52    /// **Language-agnostic**: All languages use "type", not "`field_type`"
53    #[serde(rename = "type")]
54    pub field_type: String,
55
56    /// Is field nullable?
57    pub nullable: bool,
58
59    /// Field description (from docstring)
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub description: Option<String>,
62
63    /// Applied directives (e.g., @deprecated)
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub directives: Option<Vec<IntermediateAppliedDirective>>,
66
67    /// Scope required to access this field (field-level access control)
68    ///
69    /// When set, users must have this scope in their JWT to query this field.
70    /// Supports patterns like "read:Type.field" or custom scopes like "hr:view_pii".
71    ///
72    /// # Example
73    ///
74    /// ```json
75    /// {
76    ///   "name": "salary",
77    ///   "type": "Int",
78    ///   "nullable": false,
79    ///   "requires_scope": "read:Employee.salary"
80    /// }
81    /// ```
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub requires_scope: Option<String>,
84
85    /// Policy when the user lacks `requires_scope`: `"reject"` (default) or `"mask"`.
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub on_deny: Option<String>,
88}
89
90// =============================================================================
91// Enum Definitions
92// =============================================================================
93
94/// GraphQL enum type definition in intermediate format.
95///
96/// Enums represent a finite set of possible values.
97///
98/// # Example JSON
99///
100/// ```json
101/// {
102///   "name": "OrderStatus",
103///   "values": [
104///     {"name": "PENDING"},
105///     {"name": "PROCESSING"},
106///     {"name": "SHIPPED", "description": "Package has been shipped"},
107///     {"name": "DELIVERED"}
108///   ],
109///   "description": "Possible states of an order"
110/// }
111/// ```
112#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
113pub struct IntermediateEnum {
114    /// Enum type name (e.g., "OrderStatus")
115    pub name: String,
116
117    /// Possible values for this enum
118    pub values: Vec<IntermediateEnumValue>,
119
120    /// Enum description (from docstring)
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub description: Option<String>,
123}
124
125/// A single value within an enum type.
126///
127/// # Example JSON
128///
129/// ```json
130/// {
131///   "name": "ACTIVE",
132///   "description": "The item is currently active",
133///   "deprecated": {"reason": "Use ENABLED instead"}
134/// }
135/// ```
136#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
137pub struct IntermediateEnumValue {
138    /// Value name (e.g., "PENDING")
139    pub name: String,
140
141    /// Value description (from docstring)
142    #[serde(skip_serializing_if = "Option::is_none")]
143    pub description: Option<String>,
144
145    /// Deprecation info (if value is deprecated)
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub deprecated: Option<IntermediateDeprecation>,
148}
149
150/// Deprecation information for enum values or input fields.
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
152pub struct IntermediateDeprecation {
153    /// Deprecation reason (what to use instead)
154    #[serde(skip_serializing_if = "Option::is_none")]
155    pub reason: Option<String>,
156}
157
158// =============================================================================
159// Custom Scalar Definitions
160// =============================================================================
161
162/// Custom scalar type definition in intermediate format.
163///
164/// Custom scalars allow applications to define domain-specific types with validation.
165/// Scalars are defined in language SDKs (Python, TypeScript, Java, Go, Rust)
166/// and compiled into the schema.
167///
168/// # Example JSON
169///
170/// ```json
171/// {
172///   "name": "Email",
173///   "description": "Valid email address",
174///   "specified_by_url": "https://tools.ietf.org/html/rfc5322",
175///   "base_type": "String",
176///   "validation_rules": [
177///     {
178///       "type": "pattern",
179///       "value": {
180///         "pattern": "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$"
181///       }
182///     }
183///   ]
184/// }
185/// ```
186#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
187pub struct IntermediateScalar {
188    /// Scalar name (e.g., "Email", "Phone", "ISBN")
189    pub name: String,
190
191    /// Scalar description
192    #[serde(skip_serializing_if = "Option::is_none")]
193    pub description: Option<String>,
194
195    /// URL to specification/RFC (GraphQL spec §3.5.1)
196    #[serde(skip_serializing_if = "Option::is_none")]
197    pub specified_by_url: Option<String>,
198
199    /// Built-in validation rules
200    #[serde(default)]
201    pub validation_rules: Vec<ValidationRule>,
202
203    /// Base type for type aliases (e.g., "String" for Email scalar)
204    #[serde(skip_serializing_if = "Option::is_none")]
205    pub base_type: Option<String>,
206}