graphql_client_codegen/
codegen.rs

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
mod enums;
mod inputs;
mod selection;
mod shared;

use crate::{
    query::*,
    schema::{InputId, TypeId},
    type_qualifiers::GraphqlTypeQualifier,
    GeneralError, GraphQLClientCodegenOptions,
};
use heck::ToSnakeCase;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use selection::*;
use std::collections::BTreeMap;

/// The main code generation function.
pub(crate) fn response_for_query(
    operation_id: OperationId,
    options: &GraphQLClientCodegenOptions,
    query: BoundQuery<'_>,
) -> Result<TokenStream, GeneralError> {
    let all_used_types = all_used_types(operation_id, &query);
    let response_derives = render_derives(options.all_response_derives());
    let variable_derives = render_derives(options.all_variable_derives());

    let scalar_definitions = generate_scalar_definitions(&all_used_types, options, query);
    let enum_definitions = enums::generate_enum_definitions(&all_used_types, options, query);
    let fragment_definitions =
        generate_fragment_definitions(&all_used_types, &response_derives, options, &query);
    let input_object_definitions = inputs::generate_input_object_definitions(
        &all_used_types,
        options,
        &variable_derives,
        &query,
    );

    let variables_struct =
        generate_variables_struct(operation_id, &variable_derives, options, &query);

    let definitions =
        render_response_data_fields(operation_id, options, &query).render(&response_derives);

    let q = quote! {
        use serde::{Serialize, Deserialize};
        use super::*;

        #[allow(dead_code)]
        type Boolean = bool;
        #[allow(dead_code)]
        type Float = f64;
        #[allow(dead_code)]
        type Int = i64;
        #[allow(dead_code)]
        type ID = String;

        #(#scalar_definitions)*

        #(#enum_definitions)*

        #(#input_object_definitions)*

        #variables_struct

        #(#fragment_definitions)*

        #definitions
    };

    Ok(q)
}

fn generate_variables_struct(
    operation_id: OperationId,
    variable_derives: &impl quote::ToTokens,
    options: &GraphQLClientCodegenOptions,
    query: &BoundQuery<'_>,
) -> TokenStream {
    if operation_has_no_variables(operation_id, query.query) {
        return quote!(
            #variable_derives
            pub struct Variables;
        );
    }

    let variable_fields = walk_operation_variables(operation_id, query.query)
        .map(|(_id, variable)| generate_variable_struct_field(variable, options, query));
    let variable_defaults =
        walk_operation_variables(operation_id, query.query).map(|(_id, variable)| {
            let method_name = format!("default_{}", variable.name);
            let method_name = Ident::new(&method_name, Span::call_site());
            let method_return_type = render_variable_field_type(variable, options, query);

            variable.default.as_ref().map(|default| {
                let value = graphql_parser_value_to_literal(
                    default,
                    variable.r#type.id,
                    variable
                        .r#type
                        .qualifiers
                        .get(0)
                        .map(|qual| !qual.is_required())
                        .unwrap_or(true),
                    query,
                );

                quote!(
                    pub fn #method_name() -> #method_return_type {
                        #value
                    }
                )
            })
        });

    let variables_struct = quote!(
        #variable_derives
        pub struct Variables {
            #(#variable_fields,)*
        }

        impl Variables {
            #(#variable_defaults)*
        }
    );

    variables_struct
}

fn generate_variable_struct_field(
    variable: &ResolvedVariable,
    options: &GraphQLClientCodegenOptions,
    query: &BoundQuery<'_>,
) -> TokenStream {
    let snake_case_name = variable.name.to_snake_case();
    let safe_name = shared::keyword_replace(&snake_case_name);
    let ident = Ident::new(&safe_name, Span::call_site());
    let annotation = shared::field_rename_annotation(&variable.name, &safe_name);
    let r#type = render_variable_field_type(variable, options, query);

    quote::quote!(#annotation pub #ident : #r#type)
}

fn generate_scalar_definitions<'a, 'schema: 'a>(
    all_used_types: &'a crate::query::UsedTypes,
    options: &'a GraphQLClientCodegenOptions,
    query: BoundQuery<'schema>,
) -> impl Iterator<Item = TokenStream> + 'a {
    all_used_types
        .scalars(query.schema)
        .map(move |(_id, scalar)| {
            let ident = syn::Ident::new(
                options.normalization().scalar_name(&scalar.name).as_ref(),
                proc_macro2::Span::call_site(),
            );

            if let Some(custom_scalars_module) = options.custom_scalars_module() {
                quote!(type #ident = #custom_scalars_module::#ident;)
            } else {
                quote!(type #ident = super::#ident;)
            }
        })
}

fn render_derives<'a>(derives: impl Iterator<Item = &'a str>) -> impl quote::ToTokens {
    let idents = derives.map(|s| {
        syn::parse_str::<syn::Path>(s)
            .map_err(|e| format!("couldn't parse {} as a derive Path: {}", s, e))
            .unwrap()
    });

    quote!(#[derive(#(#idents),*)])
}

fn render_variable_field_type(
    variable: &ResolvedVariable,
    options: &GraphQLClientCodegenOptions,
    query: &BoundQuery<'_>,
) -> TokenStream {
    let normalized_name = options
        .normalization()
        .input_name(variable.type_name(query.schema));
    let safe_name = shared::keyword_replace(normalized_name.clone());
    let full_name = Ident::new(safe_name.as_ref(), Span::call_site());

    decorate_type(&full_name, &variable.r#type.qualifiers)
}

fn decorate_type(ident: &Ident, qualifiers: &[GraphqlTypeQualifier]) -> TokenStream {
    let mut qualified = quote!(#ident);

    let mut non_null = false;

    // Note: we iterate over qualifiers in reverse because it is more intuitive. This
    // means we start from the _inner_ type and make our way to the outside.
    for qualifier in qualifiers.iter().rev() {
        match (non_null, qualifier) {
            // We are in non-null context, and we wrap the non-null type into a list.
            // We switch back to null context.
            (true, GraphqlTypeQualifier::List) => {
                qualified = quote!(Vec<#qualified>);
                non_null = false;
            }
            // We are in nullable context, and we wrap the nullable type into a list.
            (false, GraphqlTypeQualifier::List) => {
                qualified = quote!(Vec<Option<#qualified>>);
            }
            // We are in non-nullable context, but we can't double require a type
            // (!!).
            (true, GraphqlTypeQualifier::Required) => panic!("double required annotation"),
            // We are in nullable context, and we switch to non-nullable context.
            (false, GraphqlTypeQualifier::Required) => {
                non_null = true;
            }
        }
    }

    // If we are in nullable context at the end of the iteration, we wrap the whole
    // type with an Option.
    if !non_null {
        qualified = quote!(Option<#qualified>);
    }

    qualified
}

fn generate_fragment_definitions<'a>(
    all_used_types: &'a UsedTypes,
    response_derives: &'a impl quote::ToTokens,
    options: &'a GraphQLClientCodegenOptions,
    query: &'a BoundQuery<'a>,
) -> impl Iterator<Item = TokenStream> + 'a {
    all_used_types.fragment_ids().map(move |fragment_id| {
        selection::render_fragment(fragment_id, options, query).render(&response_derives)
    })
}

/// For default value constructors.
fn graphql_parser_value_to_literal<'doc, T>(
    value: &graphql_parser::query::Value<'doc, T>,
    ty: TypeId,
    is_optional: bool,
    query: &BoundQuery<'_>,
) -> TokenStream
where
    T: graphql_parser::query::Text<'doc>,
    T::Value: quote::ToTokens,
{
    use graphql_parser::query::Value;

    let inner = match value {
        Value::Boolean(b) => {
            if *b {
                quote!(true)
            } else {
                quote!(false)
            }
        }
        Value::String(s) => quote!(#s.to_string()),
        Value::Variable(_) => panic!("variable in variable"),
        Value::Null => panic!("null as default value"),
        Value::Float(f) => quote!(#f),
        Value::Int(i) => {
            let i = i.as_i64();
            quote!(#i)
        }
        Value::Enum(en) => quote!(#en),
        Value::List(inner) => {
            let elements = inner
                .iter()
                .map(|val| graphql_parser_value_to_literal(val, ty, false, query));
            quote! {
                vec![
                    #(#elements,)*
                ]
            }
        }
        Value::Object(obj) => ty
            .as_input_id()
            .map(|input_id| render_object_literal(obj, input_id, query))
            .unwrap_or_else(|| {
                quote!(compile_error!(
                    "Object literal on a non-input-object field."
                ))
            }),
    };

    if is_optional {
        quote!(Some(#inner))
    } else {
        inner
    }
}

/// For default value constructors.
fn render_object_literal<'doc, T>(
    object_map: &BTreeMap<T::Value, graphql_parser::query::Value<'doc, T>>,
    input_id: InputId,
    query: &BoundQuery<'_>,
) -> TokenStream
where
    T: graphql_parser::query::Text<'doc>,
    T::Value: quote::ToTokens,
{
    let input = query.schema.get_input(input_id);
    let constructor = Ident::new(&input.name, Span::call_site());
    let fields: Vec<TokenStream> = input
        .fields
        .iter()
        .map(|(name, r#type)| {
            let field_name = Ident::new(name, Span::call_site());
            let provided_value = object_map.get(name);
            match provided_value {
                Some(default_value) => {
                    let value = graphql_parser_value_to_literal(
                        default_value,
                        r#type.id,
                        r#type.is_optional(),
                        query,
                    );
                    quote!(#field_name: #value)
                }
                None => quote!(#field_name: None),
            }
        })
        .collect();

    quote!(#constructor {
        #(#fields,)*
    })
}