Skip to content

Commit 51c3043

Browse files
committed
Upd Book chapter
1 parent b535382 commit 51c3043

File tree

7 files changed

+90
-19
lines changed

7 files changed

+90
-19
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ endif
138138

139139
book.build:
140140
mdbook build book/ $(if $(call eq,$(out),),,-d $(out))
141+
rm -rf $(or $(out),book/_rendered)/lib.rs
141142

142143

143144
# Spellcheck Book.

book/src/types/enums.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ enum Episode {
7070
#
7171
# fn main() {}
7272
```
73-
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
73+
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase` and `none` (disables any renaming).
7474
7575

7676
### Documentation and deprecation

book/src/types/input_objects.md

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ In [Juniper], defining a [GraphQL input object][0] is quite straightforward and
1414
#[derive(GraphQLInputObject)]
1515
struct Coordinate {
1616
latitude: f64,
17-
longitude: f64
17+
longitude: f64,
1818
}
1919

2020
struct Root;
@@ -32,27 +32,48 @@ impl Root {
3232
# fn main() {}
3333
```
3434

35+
[`@oneOf`] [input objects][0] could be defined by using the [`#[derive(GraphQLInputObject)]` attribute][2] on a [Rust enum][enum]:
36+
```rust
37+
# #![expect(unused_variables, reason = "example")]
38+
# extern crate juniper;
39+
# use juniper::{GraphQLInputObject, ID};
40+
#
41+
#[derive(GraphQLInputObject)]
42+
enum UserBy {
43+
Id(ID), // Every `enum` variant declares a `Null`able input object field,
44+
Name(String), // so there is no need to use `Option<String>` explicitly.
45+
}
46+
#
47+
# fn main() {}
48+
```
49+
3550

3651
### Renaming
3752

38-
Just as with [defining GraphQL objects](objects/index.md#renaming), by default [struct] fields are converted from [Rust]'s standard `snake_case` naming convention into [GraphQL]'s `camelCase` convention:
53+
Just as with [defining GraphQL objects](objects/index.md#renaming), by default [struct] fields (or [enum] variants) are converted from [Rust]'s standard naming convention into [GraphQL]'s `camelCase` convention:
3954
```rust
4055
# extern crate juniper;
41-
# use juniper::GraphQLInputObject;
56+
# use juniper::{GraphQLInputObject, ID};
4257
#
4358
#[derive(GraphQLInputObject)]
4459
struct Person {
4560
first_name: String, // exposed as `firstName` in GraphQL schema
4661
last_name: String, // exposed as `lastName` in GraphQL schema
4762
}
63+
64+
#[derive(GraphQLInputObject)]
65+
enum UserBy {
66+
Id(ID), // exposed as `id` in GraphQL schema
67+
Name(String), // exposed as `name` in GraphQL schema
68+
}
4869
#
4970
# fn main() {}
5071
```
5172

5273
We can override the name by using the `#[graphql(name = "...")]` attribute:
5374
```rust
5475
# extern crate juniper;
55-
# use juniper::GraphQLInputObject;
76+
# use juniper::{GraphQLInputObject, ID};
5677
#
5778
#[derive(GraphQLInputObject)]
5879
#[graphql(name = "WebPerson")] // now exposed as `WebPerson` in GraphQL schema
@@ -62,14 +83,22 @@ struct Person {
6283
#[graphql(name = "websiteURL")]
6384
website_url: Option<String>, // now exposed as `websiteURL` in GraphQL schema
6485
}
86+
87+
#[derive(GraphQLInputObject)]
88+
#[graphql(name = "By")] // now exposed as `By` in GraphQL schema
89+
enum UserBy {
90+
#[graphql(name = "ID")]
91+
Id(ID), // now exposed as `ID` in GraphQL schema
92+
Name(String),
93+
}
6594
#
6695
# fn main() {}
6796
```
6897

6998
Or provide a different renaming policy for all the [struct] fields:
7099
```rust
71100
# extern crate juniper;
72-
# use juniper::GraphQLInputObject;
101+
# use juniper::{GraphQLInputObject, ID};
73102
#
74103
#[derive(GraphQLInputObject)]
75104
#[graphql(rename_all = "none")] // disables any renaming
@@ -78,18 +107,25 @@ struct Person {
78107
age: i32,
79108
website_url: Option<String>, // exposed as `website_url` in GraphQL schema
80109
}
110+
111+
#[derive(GraphQLInputObject)]
112+
#[graphql(rename_all = "none")] // disables any renaming
113+
enum UserBy {
114+
Id(ID), // exposed as `Id` in GraphQL schema
115+
Name(String), // exposed as `Name` in GraphQL schema
116+
}
81117
#
82118
# fn main() {}
83119
```
84-
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
120+
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase` and `none` (disables any renaming).
85121
86122

87123
### Documentation and deprecation
88124

89125
Similarly, [GraphQL input fields][1] may also be [documented][7] and [deprecated][9] via `#[graphql(description = "...")]` and `#[graphql(deprecated = "...")]`/[`#[deprecated]`][13] attributes:
90126
```rust
91127
# extern crate juniper;
92-
# use juniper::GraphQLInputObject;
128+
# use juniper::{GraphQLInputObject, ID};
93129
#
94130
/// This doc comment is visible only in Rust API docs.
95131
#[derive(GraphQLInputObject)]
@@ -112,6 +148,28 @@ struct Person {
112148
#[deprecated]
113149
another: Option<f64>, // has no description in GraphQL schema
114150
}
151+
152+
/// This doc comment is visible only in Rust API docs.
153+
#[derive(GraphQLInputObject)]
154+
#[graphql(description = "This description is visible only in GraphQL schema.")]
155+
enum UserBy {
156+
/// This doc comment is visible only in Rust API docs.
157+
#[graphql(desc = "This description is visible only in GraphQL schema.")]
158+
// ^^^^ shortcut for a `description` argument
159+
Id(ID),
160+
161+
/// This doc comment is visible in both Rust API docs and GraphQL schema
162+
/// descriptions.
163+
// `enum` variants represent `Null`able input fields already, so can be naturally
164+
// deprecated without any default values.
165+
#[graphql(deprecated = "Just because.")]
166+
Name(String),
167+
168+
// If no explicit deprecation reason is provided,
169+
// then the default "No longer supported" one is used.
170+
#[deprecated]
171+
Bio(String), // has no description in GraphQL schema
172+
}
115173
#
116174
# fn main() {}
117175
```
@@ -120,11 +178,11 @@ struct Person {
120178

121179
### Ignoring
122180

123-
By default, all [struct] fields are included into the generated [GraphQL input object][0] type. To prevent inclusion of a specific field annotate it with the `#[graphql(ignore)]` attribute:
181+
By default, all [struct] fields (or [enum] variants) are included into the generated [GraphQL input object][0] type. To prevent inclusion of a specific field/variant annotate it with the `#[graphql(ignore)]` attribute:
124182
> **WARNING**: Ignored fields must either implement `Default` or be annotated with the `#[graphql(default = <expression>)]` argument.
125183
```rust
126184
# extern crate juniper;
127-
# use juniper::GraphQLInputObject;
185+
# use juniper::{GraphQLInputObject, ID};
128186
#
129187
enum System {
130188
Cartesian,
@@ -146,6 +204,15 @@ struct Point2D {
146204
// ^^^^ alternative naming, up to your preference
147205
shift: f64,
148206
}
207+
208+
#[derive(GraphQLInputObject)]
209+
enum UserBy {
210+
Id(ID),
211+
// Ignored `enum` variants naturally doesn't require `Default` implementation or
212+
// `default` value being specified, as they're just never constructed from an input.
213+
#[graphql(ignore)]
214+
Name(String),
215+
}
149216
#
150217
# fn main() {}
151218
```
@@ -154,6 +221,9 @@ struct Point2D {
154221
155222

156223

224+
225+
[`@oneOf`]: https://spec.graphql.org/September2025#sec--oneOf
226+
[enum]: https://doc.rust-lang.org/stable/reference/items/enumerations.html
157227
[GraphQL]: https://graphql.org
158228
[Juniper]: https://docs.rs/juniper
159229
[Rust]: https://www.rust-lang.org

book/src/types/interfaces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ trait Person {
312312
#
313313
# fn main() {}
314314
```
315-
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
315+
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase` and `none` (disables any renaming).
316316
317317

318318
### Documentation and deprecation

book/src/types/objects/complex_fields.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Person {
111111
#
112112
# fn main() {}
113113
```
114-
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
114+
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase` and `none` (disables any renaming).
115115
116116

117117
### Documentation and deprecation

book/src/types/objects/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct Person {
123123
#
124124
# fn main() {}
125125
```
126-
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
126+
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase` and `none` (disables any renaming).
127127
128128

129129
### Deprecation

juniper_codegen/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ use self::common::diagnostic::{self, ResultExt as _};
137137
///
138138
/// #[derive(GraphQLInputObject)]
139139
/// enum UserBy { // `@oneOF` input objects are defined as `enum`s.
140-
/// Id(ID), // Every `enum` variant declares a `null`able input object field,
140+
/// Id(ID), // Every `enum` variant declares a `Null`able input object field,
141141
/// Name(String), // so there is no need to use `Option<String>` explicitly.
142142
/// }
143143
/// ```
@@ -202,7 +202,7 @@ use self::common::diagnostic::{self, ResultExt as _};
202202
/// // If no explicit deprecation reason is provided,
203203
/// // then the default "No longer supported" one is used.
204204
/// #[deprecated]
205-
/// Date(String), // has no description in GraphQL schema
205+
/// Bio(String), // has no description in GraphQL schema
206206
/// }
207207
/// ```
208208
///
@@ -354,7 +354,7 @@ pub fn derive_input_object(input: TokenStream) -> TokenStream {
354354
///
355355
/// However, if you need for some reason another naming convention, it's
356356
/// possible to do so by using the `rename_all` attribute's argument. At the
357-
/// moment, it supports the following policies only: `SCREAMING_SNAKE_CASE`,
357+
/// moment, it supports the following policies only: `SCREAMING_SNAKE_CASE`, `snake_case`,
358358
/// `camelCase`, `none` (disables any renaming).
359359
///
360360
/// ```rust
@@ -1246,7 +1246,7 @@ pub fn derive_scalar_value(input: TokenStream) -> TokenStream {
12461246
///
12471247
/// However, if you need for some reason apply another naming convention, it's
12481248
/// possible to do by using `rename_all` attribute's argument. At the moment it
1249-
/// supports the following policies only: `SCREAMING_SNAKE_CASE`, `camelCase`,
1249+
/// supports the following policies only: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase`,
12501250
/// `none` (disables any renaming).
12511251
///
12521252
/// ```rust
@@ -1563,7 +1563,7 @@ pub fn derive_interface(body: TokenStream) -> TokenStream {
15631563
///
15641564
/// However, if you need for some reason apply another naming convention, it's
15651565
/// possible to do by using `rename_all` attribute's argument. At the moment it
1566-
/// supports the following policies only: `SCREAMING_SNAKE_CASE`, `camelCase`,
1566+
/// supports the following policies only: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase`,
15671567
/// `none` (disables any renaming).
15681568
///
15691569
/// ```
@@ -1794,7 +1794,7 @@ pub fn derive_object(body: TokenStream) -> TokenStream {
17941794
///
17951795
/// However, if you need for some reason apply another naming convention, it's
17961796
/// possible to do by using `rename_all` attribute's argument. At the moment it
1797-
/// supports the following policies only: `SCREAMING_SNAKE_CASE`, `camelCase`,
1797+
/// supports the following policies only: `SCREAMING_SNAKE_CASE`, `snake_case`, `camelCase`,
17981798
/// `none` (disables any renaming).
17991799
///
18001800
/// ```

0 commit comments

Comments
 (0)