subgraph/graphql/entity/create_field/resolve_fields/resolve_sql_field/
mod.rs1use log::{debug, error};
2use sqlx::Row;
3
4use crate::{data_sources::sql::services::ResponseRow, graphql::entity::ServiceEntity};
5
6impl ServiceEntity {
7 pub fn resolve_sql_string_scalar(
9 response_row: &ResponseRow,
10 field_name: &str,
11 ) -> Result<Option<String>, async_graphql::Error> {
12 debug!("Resolving SQL String Scalar");
13
14 let value = match response_row {
15 ResponseRow::MySql(row) => {
16 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
17 error!("Error resolving String field: {:?}", e.to_string());
18 async_graphql::Error::new(format!(
19 "Error resolving String field: {:?}",
20 e.to_string()
21 ))
22 })?;
23 value.map(|s| s.to_string())
24 }
25 ResponseRow::SqLite(row) => {
26 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
27 error!("Error resolving String field: {:?}", e.to_string());
28 async_graphql::Error::new(format!(
29 "Error resolving String field: {:?}",
30 e.to_string()
31 ))
32 })?;
33 value.map(|s| s.to_string())
34 }
35 ResponseRow::Postgres(row) => {
36 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
37 error!("Error resolving String field: {:?}", e.to_string());
38 async_graphql::Error::new(format!(
39 "Error resolving String field: {:?}",
40 e.to_string()
41 ))
42 })?;
43 value.map(|s| s.to_string())
44 }
45 };
46
47 match value {
48 Some(value) => Ok(Some(value)),
49 None => Ok(None),
50 }
51 }
52
53 pub fn resolve_sql_int_scalar(
54 response_row: &ResponseRow,
55 field_name: &str,
56 ) -> Result<Option<i32>, async_graphql::Error> {
57 debug!("Resolving SQL Int Scalar");
58
59 let value = match response_row {
60 ResponseRow::MySql(row) => {
61 let value = match row.try_get_unchecked::<Option<i32>, _>(field_name) {
62 Ok(value) => value,
63 Err(_) => {
64 let value: Option<i64> = row.try_get(field_name).map_err(|e| {
65 error!("Error resolving Int field: {:?}", e.to_string());
66 async_graphql::Error::new(format!(
67 "Error resolving Int field: {:?}",
68 e.to_string()
69 ))
70 })?;
71 value.map(|v| v as i32)
72 }
73 };
74 value
75 }
76 ResponseRow::SqLite(row) => {
77 let value: Option<i32> = row.try_get(field_name).map_err(|e| {
78 error!("Error resolving Int field: {:?}", e.to_string());
79 async_graphql::Error::new(format!(
80 "Error resolving Int field: {:?}",
81 e.to_string()
82 ))
83 })?;
84 value
85 }
86 ResponseRow::Postgres(row) => {
87 let value: Option<i32> = row.try_get(field_name).map_err(|e| {
88 error!("Error resolving Int field: {:?}", e.to_string());
89 async_graphql::Error::new(format!(
90 "Error resolving Int field: {:?}",
91 e.to_string()
92 ))
93 })?;
94 value
95 }
96 };
97
98 match value {
99 Some(value) => Ok(Some(value)),
100 None => Ok(None),
101 }
102 }
103
104 pub fn resolve_sql_bool_scalar(
105 response_row: &ResponseRow,
106 field_name: &str,
107 ) -> Result<Option<bool>, async_graphql::Error> {
108 debug!("Resolving SQL Bool Scalar");
109
110 let value = match response_row {
111 ResponseRow::MySql(row) => {
112 let value: Option<bool> = row.try_get(field_name).map_err(|e| {
113 error!("Error resolving Bool field: {:?}", e.to_string());
114 async_graphql::Error::new(format!(
115 "Error resolving Bool field: {:?}",
116 e.to_string()
117 ))
118 })?;
119 value
120 }
121 ResponseRow::SqLite(row) => {
122 let value: Option<bool> = row.try_get(field_name).map_err(|e| {
123 error!("Error resolving Bool field: {:?}", e.to_string());
124 async_graphql::Error::new(format!(
125 "Error resolving Bool field: {:?}",
126 e.to_string()
127 ))
128 })?;
129 value
130 }
131 ResponseRow::Postgres(row) => {
132 let value: Option<bool> = row.try_get(field_name).map_err(|e| {
133 error!("Error resolving Bool field: {:?}", e.to_string());
134 async_graphql::Error::new(format!(
135 "Error resolving Bool field: {:?}",
136 e.to_string()
137 ))
138 })?;
139 value
140 }
141 };
142
143 match value {
144 Some(value) => Ok(Some(value)),
145 None => Ok(None),
146 }
147 }
148
149 pub fn resolve_sql_uuid_scalar(
150 response_row: &ResponseRow,
151 field_name: &str,
152 ) -> Result<Option<uuid::Uuid>, async_graphql::Error> {
153 debug!("Resolving SQL UUID Scalar");
154
155 let value = match response_row {
156 ResponseRow::MySql(row) => {
157 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
158 error!("Error resolving UUID field: {:?}", e.to_string());
159 async_graphql::Error::new(format!(
160 "Error resolving UUID field: {:?}",
161 e.to_string()
162 ))
163 })?;
164 match value {
165 Some(value) => Some(uuid::Uuid::parse_str(value).map_err(|e| {
166 error!("Error resolving UUID field: {:?}", e.to_string());
167 async_graphql::Error::new(format!(
168 "Error resolving UUID field: {:?}",
169 e.to_string()
170 ))
171 })?),
172 None => None,
173 }
174 }
175 ResponseRow::SqLite(row) => {
176 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
177 error!("Error resolving UUID field: {:?}", e.to_string());
178 async_graphql::Error::new(format!(
179 "Error resolving UUID field: {:?}",
180 e.to_string()
181 ))
182 })?;
183 match value {
184 Some(value) => Some(uuid::Uuid::parse_str(value).map_err(|e| {
185 error!("Error resolving UUID field: {:?}", e.to_string());
186 async_graphql::Error::new(format!(
187 "Error resolving UUID field: {:?}",
188 e.to_string()
189 ))
190 })?),
191 None => None,
192 }
193 }
194 ResponseRow::Postgres(row) => {
195 let value = row
196 .try_get(field_name)
197 .map(|value: uuid::Uuid| Some(value.to_string()))
198 .map_err(|e| {
199 error!("Error resolving UUID field: {:?}", e.to_string());
200 async_graphql::Error::new(format!(
201 "Error resolving UUID field: {:?}",
202 e.to_string()
203 ))
204 })?;
205 match value {
206 Some(value) => Some(uuid::Uuid::parse_str(&value).map_err(|e| {
207 error!("Error resolving UUID field: {:?}", e.to_string());
208 async_graphql::Error::new(format!(
209 "Error resolving UUID field: {:?}",
210 e.to_string()
211 ))
212 })?),
213 None => None,
214 }
215 }
216 };
217
218 match value {
219 Some(value) => Ok(Some(value)),
220 None => Ok(None),
221 }
222 }
223
224 pub fn resolve_sql_datetime_scalar(
225 response_row: &ResponseRow,
226 field_name: &str,
227 ) -> Result<Option<chrono::DateTime<chrono::Utc>>, async_graphql::Error> {
228 debug!("Resolving SQL DateTime Scalar");
229
230 let value = match response_row {
231 ResponseRow::MySql(row) => {
232 let value: Option<chrono::DateTime<chrono::Utc>> =
233 row.try_get(field_name).map_err(|e| {
234 error!("Error resolving DateTime field: {:?}", e.to_string());
235 async_graphql::Error::new(format!(
236 "Error resolving DateTime field: {:?}",
237 e.to_string()
238 ))
239 })?;
240 value
241 }
242 ResponseRow::SqLite(row) => {
243 let value = row.try_get(field_name);
244 match value {
245 Ok(value) => Some(value),
246 Err(_) => None,
247 }
248 }
249 ResponseRow::Postgres(row) => {
250 let value: Option<chrono::DateTime<chrono::Utc>> =
251 row.try_get(field_name).map_err(|e| {
252 error!("Error resolving DateTime field: {:?}", e.to_string());
253 async_graphql::Error::new(format!(
254 "Error resolving DateTime field: {:?}",
255 e.to_string()
256 ))
257 })?;
258 value
259 }
260 };
261
262 match value {
263 Some(value) => Ok(Some(value)),
264 None => Ok(None),
265 }
266 }
267
268 pub fn resolve_sql_enum_scalar(
269 response_row: &ResponseRow,
270 field_name: &str,
271 ) -> Result<Option<String>, async_graphql::Error> {
272 debug!("Resolving SQL Enum Scalar");
273
274 let value = match response_row {
275 ResponseRow::MySql(row) => {
276 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
277 error!("Error resolving Enum field: {:?}", e.to_string());
278 async_graphql::Error::new(format!(
279 "Error resolving Enum field: {:?}",
280 e.to_string()
281 ))
282 })?;
283 value.map(|s| s.to_string())
284 }
285 ResponseRow::SqLite(row) => {
286 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
287 error!("Error resolving Enum field: {:?}", e.to_string());
288 async_graphql::Error::new(format!(
289 "Error resolving Enum field: {:?}",
290 e.to_string()
291 ))
292 })?;
293 value.map(|s| s.to_string())
294 }
295 ResponseRow::Postgres(row) => {
296 let value: Option<&str> = row.try_get(field_name).map_err(|e| {
297 error!("Error resolving Enum field: {:?}", e.to_string());
298 async_graphql::Error::new(format!(
299 "Error resolving Enum field: {:?}",
300 e.to_string()
301 ))
302 })?;
303 value.map(|s| s.to_string())
304 }
305 };
306
307 match value {
308 Some(value) => Ok(Some(value)),
309 None => Ok(None),
310 }
311 }
312}