1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use fraiseql_core::schema::{
CompiledSchema, FieldDefinition, FieldDenyPolicy, FieldType, InterfaceDefinition,
TypeDefinition,
};
/// Inject PageInfo, Node interface, and XxxConnection/XxxEdge types for each relay type.
///
/// For each type `T` with `relay = true`:
/// - Adds `"Node"` to `T.implements` (if not already present)
/// - Generates `TEdge { cursor: String!, node: T! }`
/// - Generates `TConnection { edges: [TEdge!]!, pageInfo: PageInfo! }`
///
/// Also generates:
/// - `PageInfo` type (once): `{ hasNextPage: Boolean!, hasPreviousPage: Boolean!, startCursor:
/// String, endCursor: String }`
/// - `Node` interface (once): `{ id: ID! }`
pub(super) fn inject_relay_types(schema: &mut CompiledSchema) -> anyhow::Result<()> {
// Collect relay type names (those with relay=true).
let relay_types: Vec<String> =
schema.types.iter().filter(|t| t.relay).map(|t| t.name.to_string()).collect();
if relay_types.is_empty() {
return Ok(());
}
// Enforce the Relay Node contract before forcing `implements Node`: a relay
// type that cannot back `id: ID!` would otherwise yield a compiled schema that
// `validate()` rejects with a swallowed "missing field 'id'".
super::interface_conformance::enforce_node_id_conformance(
schema.types.iter().filter(|t| t.relay),
"relay = true requires `id: ID!` on every Relay type (the Relay Node interface requires it)",
"remove `relay = true` from them",
)?;
// --- Node interface (inject once if not already present) ---
let has_node_interface = schema.interfaces.iter().any(|i| i.name == "Node");
if !has_node_interface {
let node_id_field = FieldDefinition {
name: "id".into(),
field_type: FieldType::Id,
nullable: false,
description: Some("Globally unique identifier (UUID).".to_string()),
default_value: None,
vector_config: None,
alias: None,
deprecation: None,
requires_scope: None,
on_deny: FieldDenyPolicy::default(),
authorize: false,
encryption: None,
hierarchy: None,
};
schema.interfaces.push(
InterfaceDefinition::new("Node")
.with_description("Relay Node interface — types with a globally unique ID.")
.with_field(node_id_field),
);
}
// --- PageInfo type (inject once if not already present) ---
let has_page_info = schema.types.iter().any(|t| t.name == "PageInfo");
if !has_page_info {
let make_field = |name: &str, ft: FieldType, nullable: bool, desc: &str| FieldDefinition {
name: name.into(),
field_type: ft,
nullable,
description: Some(desc.to_string()),
default_value: None,
vector_config: None,
alias: None,
deprecation: None,
requires_scope: None,
on_deny: FieldDenyPolicy::default(),
authorize: false,
encryption: None,
hierarchy: None,
};
let page_info = TypeDefinition {
name: "PageInfo".into(),
sql_source: String::new().into(), // synthetic — no DB source
jsonb_column: String::new(),
fields: vec![
make_field(
"hasNextPage",
FieldType::Boolean,
false,
"Whether there are more items after the current page.",
),
make_field(
"hasPreviousPage",
FieldType::Boolean,
false,
"Whether there are more items before the current page.",
),
make_field(
"startCursor",
FieldType::String,
true,
"Cursor for the first item in the current page.",
),
make_field(
"endCursor",
FieldType::String,
true,
"Cursor for the last item in the current page.",
),
],
description: Some("Relay pagination info.".to_string()),
sql_projection_hint: None,
implements: Vec::new(),
requires_role: None,
is_error: false,
relay: false,
relationships: Vec::new(),
};
schema.types.push(page_info);
}
// --- Add "Node" to implements of each relay type ---
for type_def in &mut schema.types {
if type_def.relay && !type_def.implements.iter().any(|i| i == "Node") {
type_def.implements.push("Node".to_string());
}
}
// --- Generate XxxEdge and XxxConnection for each relay type ---
let make_field = |name: &str, ft: FieldType, nullable: bool, desc: &str| FieldDefinition {
name: name.into(),
field_type: ft,
nullable,
description: Some(desc.to_string()),
default_value: None,
vector_config: None,
alias: None,
deprecation: None,
requires_scope: None,
on_deny: FieldDenyPolicy::default(),
authorize: false,
encryption: None,
hierarchy: None,
};
let mut new_types: Vec<TypeDefinition> = Vec::new();
for type_name in &relay_types {
let edge_name = format!("{type_name}Edge");
let conn_name = format!("{type_name}Connection");
// Only inject if not already defined (allow user overrides).
let has_edge = schema.types.iter().any(|t| t.name == edge_name);
if !has_edge {
new_types.push(TypeDefinition {
name: edge_name.clone().into(),
sql_source: String::new().into(),
jsonb_column: String::new(),
fields: vec![
make_field(
"cursor",
FieldType::String,
false,
"Opaque pagination cursor (base64-encoded BIGINT pk).",
),
// nullable: true — per Relay spec an edge node may be null
// when the underlying object is deleted or access is denied.
make_field(
"node",
FieldType::Object(type_name.clone()),
true,
"The item at this edge.",
),
],
description: Some(format!("An edge in the {type_name} Relay connection.")),
sql_projection_hint: None,
implements: Vec::new(),
requires_role: None,
is_error: false,
relay: false,
relationships: Vec::new(),
});
}
let has_conn = schema.types.iter().any(|t| t.name == conn_name);
if !has_conn {
new_types.push(TypeDefinition {
name: conn_name.into(),
sql_source: String::new().into(),
jsonb_column: String::new(),
fields: vec![
make_field(
"edges",
FieldType::List(Box::new(FieldType::Object(edge_name))),
false,
"List of edges.",
),
make_field(
"pageInfo",
FieldType::Object("PageInfo".to_string()),
false,
"Pagination metadata.",
),
make_field(
"totalCount",
FieldType::Int,
true,
"Total number of items matching the filter.",
),
],
description: Some(format!(
"A Relay connection for paginating {type_name} records."
)),
sql_projection_hint: None,
implements: Vec::new(),
requires_role: None,
is_error: false,
relay: false,
relationships: Vec::new(),
});
}
}
schema.types.extend(new_types);
Ok(())
}