async_graphql_utils/mongo/
mount_project.rs

1use mongodb::bson::{Document, doc};
2use async_graphql::SelectionField;
3
4
5pub fn mount_project(fields: &SelectionField, project: &mut Document, father_field: Option<&str>) {
6    let c = fields.selection_set();
7
8    let mut id_exists: bool = false;
9
10    let mut child_project: Document = doc! {};
11
12    for i in c {
13        let field: &str = i.name();
14
15        if father_field.is_none() {
16            if field == "id" {
17                id_exists = true;
18            } else {
19                project.insert(field, 1);
20            }
21        } else {
22            child_project.insert(field, 1);
23        }
24
25        for _ in i.selection_set() {
26            mount_project(&i, project, Some(field));
27            break;
28        }
29    }
30
31    if !child_project.is_empty() && father_field.is_some(){
32        project.insert(father_field.unwrap(), child_project);
33    }
34
35    if id_exists {
36        project.insert("_id", 1);
37    } else {
38        project.insert("_id", 0);
39    }
40}