juniper_codegen/graphql_object/
attr.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
//! Code generation for `#[graphql_object]` macro.

use std::{any::TypeId, marker::PhantomData, mem};

use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{ext::IdentExt as _, parse_quote, spanned::Spanned};

use crate::common::{
    diagnostic, field,
    parse::{self, TypeExt as _},
    path_eq_single, rename, scalar, SpanContainer,
};

use super::{Attr, Definition, Query};

/// [`diagnostic::Scope`] of errors for `#[graphql_object]` macro.
const ERR: diagnostic::Scope = diagnostic::Scope::ObjectAttr;

/// Expands `#[graphql_object]` macro into generated code.
pub fn expand(attr_args: TokenStream, body: TokenStream) -> syn::Result<TokenStream> {
    if let Ok(mut ast) = syn::parse2::<syn::ItemImpl>(body) {
        if ast.trait_.is_none() {
            let impl_attrs = parse::attr::unite(("graphql_object", &attr_args), &ast.attrs);
            ast.attrs = parse::attr::strip(["graphql_object", "graphql"], ast.attrs);
            return expand_on_impl::<Query>(
                Attr::from_attrs(["graphql_object", "graphql"], &impl_attrs)?,
                ast,
            );
        }
    }

    Err(syn::Error::new(
        Span::call_site(),
        "#[graphql_object] attribute is applicable to non-trait `impl` blocks only",
    ))
}

/// Expands `#[graphql_object]` macro placed on an implementation block.
pub(crate) fn expand_on_impl<Operation>(
    attr: Attr,
    mut ast: syn::ItemImpl,
) -> syn::Result<TokenStream>
where
    Definition<Operation>: ToTokens,
    Operation: 'static,
{
    let type_span = ast.self_ty.span();
    let type_ident = ast.self_ty.topmost_ident().ok_or_else(|| {
        ERR.custom_error(type_span, "could not determine ident for the `impl` type")
    })?;

    let name = attr
        .name
        .clone()
        .map(SpanContainer::into_inner)
        .unwrap_or_else(|| type_ident.unraw().to_string());
    if !attr.is_internal && name.starts_with("__") {
        ERR.no_double_underscore(
            attr.name
                .as_ref()
                .map(SpanContainer::span_ident)
                .unwrap_or_else(|| type_ident.span()),
        );
    }

    let scalar = scalar::Type::parse(attr.scalar.as_deref(), &ast.generics);

    diagnostic::abort_if_dirty();

    let renaming = attr
        .rename_fields
        .as_deref()
        .copied()
        .unwrap_or(rename::Policy::CamelCase);

    let async_only = TypeId::of::<Operation>() != TypeId::of::<Query>();
    let fields: Vec<_> = ast
        .items
        .iter_mut()
        .filter_map(|item| {
            if let syn::ImplItem::Fn(m) = item {
                parse_field(m, async_only, &renaming)
            } else {
                None
            }
        })
        .collect();

    diagnostic::abort_if_dirty();

    if fields.is_empty() {
        ERR.emit_custom(type_span, "must have at least one field");
    }
    if !field::all_different(&fields) {
        ERR.emit_custom(type_span, "must have a different name for each field");
    }

    diagnostic::abort_if_dirty();

    let context = attr
        .context
        .as_deref()
        .cloned()
        .or_else(|| {
            fields.iter().find_map(|f| {
                f.arguments.as_ref().and_then(|f| {
                    f.iter()
                        .find_map(field::MethodArgument::context_ty)
                        .cloned()
                })
            })
        })
        .unwrap_or_else(|| parse_quote! { () });

    let generated_code = Definition::<Operation> {
        name,
        ty: ast.self_ty.unparenthesized().clone(),
        generics: ast.generics.clone(),
        description: attr.description.map(SpanContainer::into_inner),
        context,
        scalar,
        fields,
        interfaces: attr
            .interfaces
            .iter()
            .map(|ty| ty.as_ref().clone())
            .collect(),
        _operation: PhantomData,
    };

    Ok(quote! {
        // Omit enforcing `# Errors` and `# Panics` sections in GraphQL descriptions.
        #[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
        #ast
        #generated_code
    })
}

/// Parses a [`field::Definition`] from the given Rust [`syn::ImplItemFn`].
///
/// Returns [`None`] if parsing fails, or the method field is ignored.
#[must_use]
fn parse_field(
    method: &mut syn::ImplItemFn,
    async_only: bool,
    renaming: &rename::Policy,
) -> Option<field::Definition> {
    let method_attrs = method.attrs.clone();

    // Remove repeated attributes from the method, to omit incorrect expansion.
    method.attrs = mem::take(&mut method.attrs)
        .into_iter()
        .filter(|attr| !path_eq_single(attr.path(), "graphql"))
        .collect();

    let attr = field::Attr::from_attrs("graphql", &method_attrs)
        .map_err(diagnostic::emit_error)
        .ok()?;

    if attr.ignore.is_some() {
        return None;
    }

    if async_only && method.sig.asyncness.is_none() {
        return err_no_sync_resolvers(&method.sig);
    }

    let method_ident = &method.sig.ident;

    let name = attr
        .name
        .as_ref()
        .map(|m| m.as_ref().value())
        .unwrap_or_else(|| renaming.apply(&method_ident.unraw().to_string()));
    if name.starts_with("__") {
        ERR.no_double_underscore(
            attr.name
                .as_ref()
                .map(SpanContainer::span_ident)
                .unwrap_or_else(|| method_ident.span()),
        );
        return None;
    }

    let arguments = {
        if let Some(arg) = method.sig.inputs.first() {
            match arg {
                syn::FnArg::Receiver(rcv) => {
                    if rcv.reference.is_none() || rcv.mutability.is_some() {
                        return err_invalid_method_receiver(rcv);
                    }
                }
                syn::FnArg::Typed(arg) => {
                    if let syn::Pat::Ident(a) = &*arg.pat {
                        if a.ident == "self" {
                            return err_invalid_method_receiver(arg);
                        }
                    }
                }
            }
        }
        method
            .sig
            .inputs
            .iter_mut()
            .filter_map(|arg| match arg {
                syn::FnArg::Receiver(_) => None,
                syn::FnArg::Typed(arg) => field::MethodArgument::parse(arg, renaming, &ERR),
            })
            .collect()
    };

    let mut ty = match &method.sig.output {
        syn::ReturnType::Default => parse_quote! { () },
        syn::ReturnType::Type(_, ty) => ty.unparenthesized().clone(),
    };
    ty.lifetimes_anonymized();

    Some(field::Definition {
        name,
        ty,
        description: attr.description.map(SpanContainer::into_inner),
        deprecated: attr.deprecated.map(SpanContainer::into_inner),
        ident: method_ident.clone(),
        arguments: Some(arguments),
        has_receiver: method.sig.receiver().is_some(),
        is_async: method.sig.asyncness.is_some(),
    })
}

/// Emits "invalid method receiver" [`syn::Error`] pointing to the given `span`.
#[must_use]
fn err_invalid_method_receiver<T, S: Spanned>(span: &S) -> Option<T> {
    ERR.emit_custom(
        span.span(),
        "method should have a shared reference receiver `&self`, or no receiver at all",
    );
    None
}

/// Emits "synchronous resolvers are not supported" [`syn::Error`] pointing to
/// the given `span`.
#[must_use]
fn err_no_sync_resolvers<T, S: Spanned>(span: &S) -> Option<T> {
    ERR.custom(span.span(), "synchronous resolvers are not supported")
        .note("Specify that this function is async: `async fn foo()`")
        .emit();
    None
}