devoyage-subgraph 0.0.15

Subgraph is a CLI that instantly generates a GraphQL API around Mongo, SQL, and HTTP APIs.
Documentation
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use async_graphql::{ErrorExtensions, SelectionField};
use bson::{to_document, Document};
use evalexpr::{eval_with_context_mut, HashMapContext};
use http::HeaderMap;
use log::{debug, error, trace};
use serde_json::Value;

use crate::{
    configuration::subgraph::{
        entities::{service_entity_field::ServiceEntityFieldConfig, ServiceEntityConfig},
        guard::{
            guard_data_context::{GuardDataContext, VariablePair},
            Guard,
        },
        SubGraphConfig,
    },
    data_sources::{sql::services::ResponseRow, DataSources},
    graphql::{
        entity::create_return_types::ResolverResponse, schema::create_auth_service::TokenData,
    },
    resolver_type::ResolverType,
    utils::clean_string::clean_string,
};

use super::ServiceResolver;

impl ServiceResolver {
    pub async fn guard_resolver_function(
        selection_fields: Vec<SelectionField<'_>>,
        input_document: &Document,
        entity: &ServiceEntityConfig,
        service_guards: Option<Vec<Guard>>,
        resolver_type: &ResolverType,
        headers: HeaderMap,
        token_data: &Option<TokenData>,
        data_sources: &DataSources,
        subgraph_config: &SubGraphConfig,
    ) -> Result<HashMapContext, async_graphql::Error> {
        debug!("Guard Resolver Function");

        // Get the guards
        let resolver_guards =
            match ServiceEntityConfig::get_resolver(&entity, resolver_type.clone()) {
                Some(resolver) => {
                    debug!(
                        "Guarding resolver {:?} for entity {:?}",
                        resolver_type, entity.name
                    );
                    resolver.guards
                }
                None => None,
            };
        let field_guards =
            ServiceResolver::get_field_guards(selection_fields, &entity, None).unwrap_or(vec![]);

        // Handle guard data contexts to inject data from the graph into the eval context.
        let guard_data_contexts = Guard::get_guard_data_contexts(
            service_guards.clone(),
            entity.guards.clone(),
            resolver_guards.clone(),
            Some(field_guards.clone()),
        );

        // Create the context to parse the variables in the the `if_expr` and the `query`
        let mut guard_context = Guard::create_guard_context(
            headers.clone(),
            token_data.clone(),
            input_document.clone(),
            resolver_type.to_string(),
            None,
            Some(guard_data_contexts.clone()),
            subgraph_config.clone(),
        )
        .map_err(|e| {
            error!("Error creating guard context: {:?}", e);
            async_graphql::Error::new("Error creating guard context")
        })?;

        // Fetch the data from the data source and return it as json
        let data_context = ServiceResolver::execute_data_context(
            guard_data_contexts.clone(),
            data_sources,
            subgraph_config,
            guard_context,
            headers.clone(),
            token_data,
            resolver_type,
            input_document.clone(),
        )
        .await
        .map_err(|e| {
            error!("Error executing data context: {:?}", e);
            async_graphql::Error::new("Error executing data context")
        })?;

        // Re-create the evalexpr context including the data context
        guard_context = Guard::create_guard_context(
            headers,
            token_data.clone(),
            input_document.clone(),
            resolver_type.to_string(),
            Some(data_context),
            Some(guard_data_contexts),
            subgraph_config.clone(),
        )?;

        // Execute the guards
        match entity.guards.clone() {
            Some(guards) => Guard::check(&guards, &mut guard_context.clone())?,
            None => (),
        };
        if service_guards.is_some() {
            Guard::check(&service_guards.unwrap(), &mut guard_context.clone())?;
        }
        if resolver_guards.is_some() {
            Guard::check(&resolver_guards.unwrap(), &mut guard_context.clone())?;
        }
        if field_guards.len() > 0 {
            Guard::check(&field_guards, &mut guard_context.clone())?;
        }

        Ok(guard_context)
    }

    /// Get entity field guards, recursively.
    pub fn get_field_guards(
        selection_fields: Vec<SelectionField>,
        entity: &ServiceEntityConfig,
        mut prefix: Option<String>,
    ) -> Result<Vec<Guard>, async_graphql::Error> {
        debug!("Getting field guards");
        let mut field_guards = vec![];

        for selection_field in selection_fields {
            if selection_field.name() == "__typename" {
                continue;
            }

            let field_name = format!(
                "{}{}",
                prefix.clone().unwrap_or("".to_string()),
                selection_field.name()
            );
            let fields = ServiceEntityFieldConfig::get_fields_recursive(
                entity.fields.clone(),
                field_name.to_string(),
            )?;

            let field = fields.iter().last().unwrap(); // Last field found will be the one we want.

            if selection_field.selection_set().count() > 0 && field.as_type.is_none() {
                prefix = Some(format!("{}.", field_name));
                for selection_field in selection_field.selection_set().into_iter() {
                    // Call this function recursively to get the field guards.
                    let mut nested_field_guards = ServiceResolver::get_field_guards(
                        vec![selection_field],
                        &entity,
                        prefix.clone(),
                    )?;
                    field_guards.append(&mut nested_field_guards);
                }
            }
        }
        trace!("Field guards: {:?}", field_guards);
        Ok(field_guards)
    }

    /// Fetches from the DB the requested data for context.
    /// Adds the data to the context.
    /// Returns the context.
    pub async fn execute_data_context(
        guard_data_contexts: Vec<GuardDataContext>,
        data_sources: &DataSources,
        subgraph_config: &SubGraphConfig,
        mut guard_context: HashMapContext,
        headers: HeaderMap,
        token_data: &Option<TokenData>,
        resolver_type: &ResolverType,
        input: Document,
    ) -> Result<Value, async_graphql::Error> {
        debug!(
            "Execute Data Contexts: {:?}",
            guard_data_contexts.iter().map(|g| {
                if g.name.is_some() {
                    g.name.clone().unwrap()
                } else {
                    g.entity_name.clone()
                }
            })
        );
        let mut data_context = serde_json::json!({});
        for guard_data_context in guard_data_contexts.clone() {
            let input_name = format!("get_{}s_input", guard_data_context.entity_name);

            // Get the entity to determine data_source.
            let entity = match subgraph_config
                .clone()
                .get_entity(&guard_data_context.entity_name)
            {
                Some(entity) => entity,
                None => {
                    return Err(async_graphql::Error::new(
                        "Can't use an entity that does not exist in guard data context."
                            .to_string(),
                    ))
                }
            };
            let mut query = guard_data_context.query.clone();
            let variables = guard_data_context.variables;

            query = ServiceResolver::replace_variables_in_query(query, variables, guard_context)?;

            let json: Value = match serde_json::from_str(&query) {
                Ok(json) => json,
                Err(error) => {
                    error!(
                        "Can't parse the guard data context query. Failed to parse JSON: {:?}",
                        error
                    );
                    return Err(async_graphql::Error::new(
                        "Can't parse the guard data context query. Failed to parse JSON."
                            .to_string(),
                    )
                    .extend_with(|_, e| e.set("error", error.to_string())));
                }
            };

            let input_document = to_document(&json).map_err(|error| {
                error!(
                    "Can't parse the guard data context query. Failed to convert to document: {:?}",
                    error
                );
                async_graphql::Error::new(
                    "Can't parse the guard data context query. Failed to convert to document."
                        .to_string(),
                )
                .extend_with(|_, e| e.set("error", error.to_string()))
            })?;

            let input_query = input_document.get(&input_name);

            if input_query.is_none() {
                error!("Can't parse the guard data context query. Failed to get input query.");
                return Err(async_graphql::Error::new(
                    "Can't parse the guard data context query. Failed to get input query."
                        .to_string(),
                ));
            }

            let input_query_document = input_query.unwrap().as_document();

            if input_query_document.is_none() {
                return Err(async_graphql::Error::new(
                    "Can't parse the guard data context query. Failed to get input query document."
                        .to_string(),
                ));
            }

            let has_selection_set = true;

            //Execute the operation to get the data.
            let results = DataSources::execute(
                &data_sources,
                input_query_document.unwrap().to_owned(),
                entity.clone(),
                ResolverType::FindMany,
                subgraph_config,
                token_data,
                has_selection_set,
            )
            .await?;

            if results.is_none() {
                debug!("No results found for data context query.");
                return Ok(data_context);
            }

            let results = results.unwrap();

            let downcasted = results.try_downcast_ref::<ResolverResponse>()?;

            // iterate and turn all to json so it can be parsed when guarding.
            let mut results_json = serde_json::json!([]);
            for result in &downcasted.data {
                let downcasted = result.try_downcast_ref::<Option<Document>>();
                if downcasted.is_err() {
                    debug!(
                        "Attempting to downcast result to Option<Document> failed. Attempting SQL Downcast.",
                    );
                    let downcasted = result.try_downcast_ref::<Option<ResponseRow>>();
                    if downcasted.is_err() {
                        error!(
                            "Failed to get result from data context query. Error: {:?}",
                            downcasted.err()
                        );
                        return Err(async_graphql::Error::new(
                            "Can't parse the guard data context query. Failed to get result."
                                .to_string(),
                        ));
                    }
                    let result = downcasted.unwrap();
                    if result.is_none() {
                        debug!("Data Context Result is none.");
                        continue;
                    }
                    let response_row = result.as_ref().unwrap();

                    let mut json_obj = serde_json::json!({});

                    for field in entity.fields.iter() {
                        if field.as_type.is_some() && field.join_from.is_some() {
                            continue;
                        }
                        if field.is_virtual.unwrap_or(false) {
                            continue;
                        }
                        let json_value = field
                            .scalar
                            .clone()
                            .rr_to_serde_json_value(response_row, &field.name)?;
                        json_obj[field.name.clone()] = json_value;
                    }

                    results_json
                        .as_array_mut()
                        .unwrap()
                        .push(serde_json::json!(json_obj));

                    continue;
                }
                let result = downcasted.unwrap().to_owned();
                if result.is_none() {
                    continue;
                }
                let result = result.unwrap();
                let json = serde_json::to_string(&result);
                if json.is_err() {
                    error!(
                        "Failed to get result from data context query. Error: {:?}",
                        json.err()
                    );
                    return Err(async_graphql::Error::new(
                        "Can't parse the guard data context query. Failed to get result."
                            .to_string(),
                    ));
                }
                let json = json.unwrap();
                results_json
                    .as_array_mut()
                    .unwrap()
                    .push(serde_json::from_str(&json).unwrap());
            }

            // If provded with `name`, use this as the key in the data context.
            // Otherwise, use the entity name.
            let key_name = if let Some(key_name) = guard_data_context.name {
                key_name
            } else {
                guard_data_context.entity_name.clone()
            };
            data_context[key_name] = results_json;

            let query_input = input_document.get(&input_name);

            if query_input.is_none() {
                return Err(async_graphql::Error::new(
                    "Can't parse the guard data context query. Failed to get input query."
                        .to_string(),
                ));
            }

            let query_input = query_input.unwrap().as_document();
            if query_input.is_none() {
                return Err(async_graphql::Error::new(
                    "Can't parse the guard data context query. Failed to get input query document."
                        .to_string(),
                ));
            }

            // Recreate the context to include the data context for subsequent guards.
            let updated_guard_context =
                match Guard::create_guard_context(
                    headers.clone(),
                    token_data.clone(),
                    input.clone(),
                    resolver_type.to_string().clone(),
                    Some(data_context.clone()),
                    Some(guard_data_contexts.clone()),
                    subgraph_config.clone(),
                ) {
                    Ok(guard_context) => guard_context,
                    Err(_) => return Err(async_graphql::Error::new(
                        "Can't parse the guard data context query. Failed to create guard context."
                            .to_string(),
                    )),
                };
            // replace the guard context with the updated one.
            guard_context = updated_guard_context;
        }

        debug!("Data Context: {:?}", data_context);
        Ok(data_context)
    }

    /// Replaces the variables in a query string with the values from the data context.
    pub fn replace_variables_in_query(
        mut query: String,
        variables: Vec<VariablePair>,
        mut guard_context: HashMapContext,
    ) -> Result<String, async_graphql::Error> {
        debug!("Creating query string for data context: {:?}", query);
        for variable in variables {
            let key = variable.0;
            let value = variable.1;

            let value = eval_with_context_mut(&value, &mut guard_context)?;

            if value.is_empty() {
                return Err(async_graphql::Error::new(
                    "Can't parse the guard data context query. Failed to parse empty value."
                        .to_string(),
                ));
            }

            // If value is a tuple, map it to json array.
            // Else simply replace.
            if value.is_string() || value.is_number() {
                let str_value = value.to_string();
                query = query.replace(&key, &str_value);
            }
            if value.is_tuple() {
                if value.as_tuple().unwrap().len() == 0 {
                    error!(
                        "Replace Variables In Query: Tuple length is 0. Query: {:?}",
                        query
                    );
                    return Err(async_graphql::Error::new(format!(
                        "Can't parse the guard data context query. Variable `{key}` not provided.",
                    ))
                    .extend_with(|_, e| e.set("query", query.clone())));
                }
                let mut json_array = serde_json::json!([]);
                // For each tuple, we need to push it into the json array as an object.
                for tuple in value.as_tuple().unwrap() {
                    let mut json_object = serde_json::json!({});

                    if tuple.is_string() {
                        let tuple_value = tuple.to_string();
                        json_object[key.clone().replace("{{", "").replace("}}", "")] =
                            serde_json::json!(clean_string(&tuple_value, None));
                    } else if tuple.is_boolean() {
                        let tuple_value = tuple.as_boolean();
                        json_object[key.clone().replace("{{", "").replace("}}", "")] =
                            serde_json::json!(tuple_value.unwrap());
                    } else if tuple.is_int() {
                        let tuple_value = tuple.as_int();
                        json_object[key.clone().replace("{{", "").replace("}}", "")] =
                            serde_json::json!(tuple_value.unwrap());
                    }

                    json_array.as_array_mut().unwrap().push(json_object);
                }
                query = query.replace(&key, &json_array.to_string());
            }
        }
        debug!("Query String with Variables Replaced: {:?}", query);
        Ok(query)
    }
}