Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit b2d1c76

Browse files
committed
Document doc comments as descriptions
Part of #8.
1 parent a0faf88 commit b2d1c76

File tree

1 file changed

+57
-12
lines changed

1 file changed

+57
-12
lines changed

docs/types/objects/defining_objects.md

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,80 @@ can use `Option<T>`.
2828

2929
We should take advantage of the
3030
fact that GraphQL is self-documenting and add descriptions to the type and
31-
fields:
31+
fields. Juniper will automatically use associated doc comments as GraphQL
32+
descriptions:
33+
34+
!FILENAME GraphQL descriptions via Rust doc comments
35+
36+
```rust
37+
# extern crate juniper;
38+
# #[macro_use] extern crate juniper_codegen;
39+
#
40+
#[derive(GraphQLObject)]
41+
/// Information about a person
42+
struct Person {
43+
/// The person's full name, including both first and last names
44+
name: String,
45+
/// The person's age in years, rounded down
46+
age: i32,
47+
}
48+
49+
# fn main() {}
50+
```
51+
52+
Objects and fields without doc comments can instead set a `description`
53+
in the `graphql` attribute. The following example is equivalent to the above:
54+
55+
!FILENAME GraphQL descriptions via attribute
3256

3357
```rust
3458
# extern crate juniper;
3559
# #[macro_use] extern crate juniper_codegen;
36-
#
60+
#
3761
#[derive(GraphQLObject)]
3862
#[graphql(description="Information about a person")]
3963
struct Person {
4064
#[graphql(description="The person's full name, including both first and last names")]
4165
name: String,
42-
4366
#[graphql(description="The person's age in years, rounded down")]
4467
age: i32,
4568
}
4669

4770
# fn main() {}
4871
```
4972

73+
Descriptions set via the `graphql` attribute take precedence over Rust
74+
doc comments. This enables internal Rust documentation and external GraphQL
75+
documentation to differ:
76+
77+
```rust
78+
# extern crate juniper;
79+
# #[macro_use] extern crate juniper_codegen;
80+
#
81+
#[derive(GraphQLObject)]
82+
#[graphql(description="This description shows up in GraphQL")]
83+
/// This description shows up in RustDoc
84+
struct Person {
85+
#[graphql(description="This description shows up in GraphQL")]
86+
/// This description shows up in RustDoc
87+
name: String,
88+
/// This description shows up in both RustDoc and GraphQL
89+
age: i32,
90+
}
91+
92+
# fn main() {}
93+
```
94+
5095
## Relationships
5196

5297
You can only use the custom derive attribute under these circumstances:
5398

54-
* The annotated type is a `struct`,
55-
* Every struct field is either
56-
* A primitive type (`i32`, `f64`, `bool`, `String`, `juniper::ID`), or
57-
* A valid custom GraphQL type, e.g. another struct marked with this attribute,
99+
- The annotated type is a `struct`,
100+
- Every struct field is either
101+
- A primitive type (`i32`, `f64`, `bool`, `String`, `juniper::ID`), or
102+
- A valid custom GraphQL type, e.g. another struct marked with this attribute,
58103
or
59-
* A container/reference containing any of the above, e.g. `Vec<T>`, `Box<T>`,
104+
- A container/reference containing any of the above, e.g. `Vec<T>`, `Box<T>`,
60105
`Option<T>`
61106

62107
Let's see what that means for building relationships between objects:
@@ -92,7 +137,7 @@ convention into GraphQL's `camelCase` convention:
92137
```rust
93138
# extern crate juniper;
94139
# #[macro_use] extern crate juniper_codegen;
95-
#
140+
#
96141
#[derive(GraphQLObject)]
97142
struct Person {
98143
first_name: String, // Would be exposed as firstName in the GraphQL schema
@@ -108,7 +153,7 @@ fields:
108153
```rust
109154
# extern crate juniper;
110155
# #[macro_use] extern crate juniper_codegen;
111-
#
156+
#
112157
#[derive(GraphQLObject)]
113158
struct Person {
114159
name: String,
@@ -128,7 +173,7 @@ attribute:
128173
```rust
129174
# extern crate juniper;
130175
# #[macro_use] extern crate juniper_codegen;
131-
#
176+
#
132177
#[derive(GraphQLObject)]
133178
struct Person {
134179
name: String,
@@ -142,4 +187,4 @@ struct Person {
142187

143188
The `name`, `description`, and `deprecation` arguments can of course be
144189
combined. Some restrictions from the GraphQL spec stil applies though; you can
145-
only deprecate object fields and enum values.
190+
only deprecate object fields and enum values.

0 commit comments

Comments
 (0)