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
use proc_macro2::TokenStream;

use super::field_selector::FieldSelector;
use super::selection_builder::FieldSelectionBuilder;

use crate::{
    schema::{self, FieldExt},
    FieldType, Ident, TypeIndex, TypePath,
};

/// We generate a SelectorStruct for each queryable object in the schema.
///
///
/// When output from our macros these contain FieldSelector functions that
/// create the selection sets which make up a graphql query.
#[derive(Debug)]
pub struct SelectorStruct {
    pub name: Ident,
    pub graphql_name: String,
    pub fields: Vec<FieldSelector>,
    pub selection_builders: Vec<FieldSelectionBuilder>,
}

impl SelectorStruct {
    pub fn from_object(obj: &schema::ObjectType, type_index: &TypeIndex) -> Self {
        SelectorStruct::new(&obj.name, &obj.fields, type_index)
    }

    pub fn new(graphql_name: &str, fields: &[schema::Field], type_index: &TypeIndex) -> Self {
        let name = Ident::for_type(graphql_name);

        let mut processed_fields = Vec::with_capacity(fields.len());
        let mut selection_builders = Vec::with_capacity(fields.len());

        for field in fields {
            let field_type = FieldType::from_schema_type(&field.field_type, type_index);

            let selection_builder = FieldSelectionBuilder::for_field(
                &field.name,
                field_type.clone(),
                name.clone(),
                field.optional_arguments(),
                type_index,
            );

            processed_fields.push(FieldSelector::for_field(
                &field.name,
                field_type,
                name.clone(),
                Ident::for_module(graphql_name),
                field.required_arguments(),
                TypePath::new(vec![
                    Ident::for_module(graphql_name),
                    selection_builder.name.clone(),
                ]),
                type_index,
            ));

            selection_builders.push(selection_builder);
        }

        SelectorStruct {
            name,
            graphql_name: graphql_name.to_owned(),
            fields: processed_fields,
            selection_builders,
        }
    }
}

impl quote::ToTokens for SelectorStruct {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        use quote::{quote, TokenStreamExt};

        let name = &self.name;
        let fields = &self.fields;

        tokens.append_all(quote! {
            #[allow(dead_code)]
            pub struct #name;

            #[allow(dead_code)]
            impl #name {
                #(
                    #fields
                )*
            }
        });
    }
}