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
//! Procedural macros for `gurafi`.
//!
//! These macros let Rust types drive the GraphQL type system:
//!
//! - [`macro@Object`] registers a Rust struct as a GraphQL object type.
//! - [`macro@Input`] registers a Rust struct as a field's arguments, or as a named input object
//! when it carries `#[graphql(name = "...")]`.
//! - [`macro@Args`] is an alias for [`macro@Input`] that says the type is only a field's
//! arguments.
//! - [`macro@Enum`] registers a Rust enum as a GraphQL enum.
//! - [`macro@Scalar`] registers a newtype as a custom scalar delegating to its inner type.
//! - [`macro@Union`] registers a Rust enum as a GraphQL union of object types.
//! - [`macro@ErrorUnion`] declares the typed errors a `field_result_union` exposes.
//!
//! Every macro emits all of the trait implementations `gurafi` needs, so a derive is the
//! whole registration: there is no second list of fields to keep in sync. Unsupported shapes
//! (generic types, tuple structs for [`macro@Input`], enums with data for [`macro@Enum`])
//! produce a `compile_error!` with an explanation instead of a confusing trait bound failure.
use TokenStream;
use ;
/// Derive a GraphQL object type.
///
/// The Rust type name becomes the GraphQL type name; override it with
/// `#[graphql(name = "...")]`. A doc comment on the type becomes its description, and
/// `#[graphql(description = "...")]` overrides the comment.
///
/// Fields are *not* derived. An object's GraphQL fields are declared explicitly on the
/// schema builder with `field`, `field_result`, `field_async`, `field_result_async` or
/// `expose`, so every field's resolver and access rules live in one place.
///
/// ```ignore
/// use gurafi::Object;
///
/// #[derive(Object)]
/// #[graphql(description = "A long-necked animal.")]
/// struct Giraffe {
/// name: String,
/// }
/// ```
/// Derive a GraphQL input type.
///
/// # Arguments by default
///
/// Without a name, the type is **args-only**: its fields become a field's arguments
/// (`A: InputObject`) and no input object is registered in the schema. This is what an
/// arguments struct should be — `friends(limit: 10, after: "...")` rather than a leaked
/// `FriendsArgs` input type.
///
/// A type that is also used nested inside another input object must be named:
///
/// ```ignore
/// use gurafi::Input;
///
/// #[derive(Input)]
/// struct FriendsArgs {
/// #[graphql(description = "Max results", default = 10)]
/// limit: i32,
/// after: Option<String>,
/// }
///
/// #[derive(Input)]
/// #[graphql(name = "FilterInput")]
/// struct Filter {
/// prefix: Option<String>,
/// }
///
/// #[derive(Input)]
/// struct SearchArgs {
/// filter: Option<Filter>, // requires FilterInput to exist
/// }
/// ```
///
/// Attribute support:
///
/// - `#[graphql(name = "...")]` makes the type a reusable named input object, or renames one of
/// its fields.
/// - `#[graphql(description = "...")]` (or a doc comment) sets a description.
/// - `#[graphql(default = expr)]` sets a default value, typed as the field's Rust type.
/// - `#[graphql(default_with = "path")]` computes the default by calling `path()`.
/// - `#[graphql(deprecated)]` / `#[graphql(deprecated = "reason")]` deprecates a field.
/// - `#[graphql(validate = "path")]` runs `path(&value)` after coercion. On the type it receives
/// `&Self`; on a field it receives `&FieldType`. The function returns
/// `Result<(), gql::GraphQLError>` and its error becomes a field error.
/// - `#[graphql(one_of)]` marks a named input as a `@oneOf` input. Every field must be
/// `Option<T>` and no field may have a default.
///
/// `Option<T>` fields are optional; every other field without a default is required.
/// Derive a field's arguments type.
///
/// This is an alias for [`macro@Input`] in its default, args-only form. It exists to make the
/// intent obvious at the declaration site; if the type later needs to be a reusable named input
/// object, add `#[graphql(name = "...")]` and the behaviour is identical to [`macro@Input`].
/// Derive a GraphQL enum.
///
/// GraphQL enum values are usually `SCREAMING_CASE`; the Rust variant name is used verbatim
/// unless `#[graphql(name = "...")]` overrides it. Variants must be unit variants.
///
/// ```ignore
/// use gurafi::Enum;
///
/// #[derive(Enum)]
/// enum Color {
/// #[graphql(name = "RED")]
/// Red,
/// Blue,
/// }
/// ```
/// Derive a custom GraphQL scalar from a newtype.
///
/// The newtype delegates to its inner type's input and value conversions, which is how a
/// validated wrapper (a date, a UUID, an email address) becomes a scalar without writing any
/// conversions by hand.
///
/// ```ignore
/// use gurafi::Scalar;
///
/// #[derive(Scalar, Clone)]
/// #[graphql(name = "Date", description = "An ISO-8601 date.")]
/// struct Date(String);
/// ```
/// Derive a GraphQL union from an enum of object models.
///
/// Each variant must be a newtype variant whose inner type derives [`macro@Object`]. The union
/// itself is still declared on the schema with `schema.union(...)`, so its members and members'
/// fields stay visible where the rest of the schema is defined.
///
/// ```ignore
/// use gurafi::{Object, Union};
///
/// #[derive(Object)]
/// struct User {
/// name: String,
/// }
///
/// #[derive(Object)]
/// struct Post {
/// title: String,
/// }
///
/// #[derive(Union)]
/// enum SearchResult {
/// User(User),
/// Post(Post),
/// }
/// ```
/// Derive the typed errors of a result-union field.
///
/// The enum is not itself a GraphQL type. It describes how a `field_result_union` field turns
/// each Rust error variant into a GraphQL object payload, and the generated result union uses
/// those payloads as members. Every variant must hold exactly one object model:
///
/// ```ignore
/// use gurafi::{ErrorUnion, Object};
///
/// #[derive(Object)]
/// struct NotFoundError {
/// message: String,
/// }
///
/// #[derive(Object)]
/// struct ConflictError {
/// field: String,
/// }
///
/// #[derive(ErrorUnion)]
/// enum CreateUserError {
/// NotFound(NotFoundError),
/// Conflict(ConflictError),
/// }
/// ```
///
/// The payload objects must still be registered with `schema.object::<M>(...)`. Missing
/// registration is a schema build error that names the missing payload. Generic enums, unit
/// variants, struct variants and tuple variants with zero or several fields are compile errors.