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
use mongodb::bson::{Document, doc};
use async_graphql::SelectionField;


pub fn mount_project(fields: &SelectionField, project: &mut Document, father_field: Option<&str>) {
    let c = fields.selection_set();

    let mut id_exists: bool = false;

    let mut child_project: Document = doc! {};

    for i in c {
        let field: &str = i.name();

        if father_field.is_none() {
            if field == "id" {
                id_exists = true;
            } else {
                project.insert(field, 1);
            }
        } else {
            child_project.insert(field, 1);
        }

        for _ in i.selection_set() {
            mount_project(&i, project, Some(field));
            break;
        }
    }

    if !child_project.is_empty() && father_field.is_some(){
        project.insert(father_field.unwrap(), child_project);
    }

    if id_exists {
        project.insert("_id", 1);
    } else {
        project.insert("_id", 0);
    }
}