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
use darling::util::SpannedValue;

#[derive(darling::FromDeriveInput)]
#[darling(attributes(cynic), supports(enum_newtype))]
pub struct InlineFragmentsDeriveInput {
    pub(super) ident: proc_macro2::Ident,
    pub(super) data: darling::ast::Data<SpannedValue<InlineFragmentsDeriveVariant>, ()>,

    pub schema_path: SpannedValue<String>,
    pub query_module: SpannedValue<String>,
    pub graphql_type: SpannedValue<String>,
    #[darling(default)]
    pub argument_struct: Option<syn::Ident>,
}

#[derive(darling::FromVariant)]
#[darling(attributes(cynic))]
pub(super) struct InlineFragmentsDeriveVariant {
    pub ident: proc_macro2::Ident,
    pub fields: darling::ast::Fields<InlineFragmentsDeriveField>,
}

#[derive(darling::FromField)]
#[darling(attributes(cynic))]
pub(super) struct InlineFragmentsDeriveField {
    pub ty: syn::Type,
}

/// An alternative FragmentDeriveInput struct that doesn't require as many fields.
///
/// This is used by the query_module generation, which provides some of the parameters
/// without the users input.
#[derive(darling::FromDeriveInput)]
#[darling(attributes(cynic), supports(enum_newtype))]
pub(crate) struct QueryModuleInlineFragmentsDeriveInput {
    ident: proc_macro2::Ident,
    data: darling::ast::Data<SpannedValue<InlineFragmentsDeriveVariant>, ()>,

    #[darling(default)]
    pub schema_path: Option<SpannedValue<String>>,
    #[darling(default)]
    pub query_module: Option<SpannedValue<String>>,
    pub graphql_type: SpannedValue<String>,
    #[darling(default)]
    pub argument_struct: Option<syn::Ident>,
}

impl QueryModuleInlineFragmentsDeriveInput {
    pub fn to_inline_fragments_derive_input(
        self,
        schema_path: &SpannedValue<String>,
        query_module: &SpannedValue<String>,
    ) -> InlineFragmentsDeriveInput {
        InlineFragmentsDeriveInput {
            ident: self.ident,
            data: self.data,
            schema_path: self.schema_path.unwrap_or_else(|| schema_path.clone()),
            query_module: self.query_module.unwrap_or_else(|| query_module.clone()),
            graphql_type: self.graphql_type,
            argument_struct: self.argument_struct,
        }
    }
}