@@ -28,35 +28,80 @@ can use `Option<T>`.
28
28
29
29
We should take advantage of the
30
30
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
32
56
33
57
``` rust
34
58
# extern crate juniper;
35
59
# #[macro_use] extern crate juniper_codegen;
36
- #
60
+ #
37
61
#[derive(GraphQLObject )]
38
62
#[graphql(description= " Information about a person" )]
39
63
struct Person {
40
64
#[graphql(description= " The person's full name, including both first and last names" )]
41
65
name : String ,
42
-
43
66
#[graphql(description= " The person's age in years, rounded down" )]
44
67
age : i32 ,
45
68
}
46
69
47
70
# fn main () {}
48
71
```
49
72
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
+
50
95
## Relationships
51
96
52
97
You can only use the custom derive attribute under these circumstances:
53
98
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,
58
103
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> ` ,
60
105
` Option<T> `
61
106
62
107
Let's see what that means for building relationships between objects:
@@ -92,7 +137,7 @@ convention into GraphQL's `camelCase` convention:
92
137
``` rust
93
138
# extern crate juniper;
94
139
# #[macro_use] extern crate juniper_codegen;
95
- #
140
+ #
96
141
#[derive(GraphQLObject )]
97
142
struct Person {
98
143
first_name : String , // Would be exposed as firstName in the GraphQL schema
@@ -108,7 +153,7 @@ fields:
108
153
``` rust
109
154
# extern crate juniper;
110
155
# #[macro_use] extern crate juniper_codegen;
111
- #
156
+ #
112
157
#[derive(GraphQLObject )]
113
158
struct Person {
114
159
name : String ,
@@ -128,7 +173,7 @@ attribute:
128
173
``` rust
129
174
# extern crate juniper;
130
175
# #[macro_use] extern crate juniper_codegen;
131
- #
176
+ #
132
177
#[derive(GraphQLObject )]
133
178
struct Person {
134
179
name : String ,
@@ -142,4 +187,4 @@ struct Person {
142
187
143
188
The ` name ` , ` description ` , and ` deprecation ` arguments can of course be
144
189
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