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
use crateGraphQLSchemaTokenConsumer;
use crateGraphQLSchemaFromStrTokenConsumer;
/// Evaluates to a [`Schema`](libgraphql_core::schema::Schema) object given
/// direct GraphQL schema document syntax.
///
/// This macro is effectively a compile-time version of
/// [`SchemaBuilder::build_from_str()`](libgraphql_core::schema::SchemaBuilder::build_from_ast()),
/// except you write GraphQL syntax in your Rust file and the macro parses it as
/// GraphQL for you.
///
/// Example usage:
///
/// ```rust
/// use libgraphql::macros::graphql_schema;
///
/// let schema = graphql_schema! {
/// type Query {
/// // This field always resolves the currently-authenticated `User`.
/// me: User,
/// }
///
/// type User {
/// firstName: String,
/// lastName: String,
/// }
/// };
///
/// let user_type =
/// schema.defined_types()
/// .get("User")
/// .unwrap()
/// .as_object()
/// .unwrap();
///
/// assert_eq!(user_type.name(), "User");
/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
/// assert_eq!(user_type.fields().get("doesntExist").is_some(), false);
/// ```
///
/// ## **⚠️ NOTE:**
///
/// Due to limitations downstream of how Rust macros tokenize syntax, there
/// are a few inline GraphQL syntax edge-cases that are not supported by this
/// macro:
///
/// 1) `#` cannot be used to specify GraphQL comments. Instead, you can use
/// Rust's `//` or `/*` comment syntax.
///
/// So for example, this won't compile:
///
/// ```rust,compile_fail
/// let schema = graphql_schema! {
/// type Query {
/// me: User,
/// }
///
/// ## Represents a user in the system.
/// type User {
/// firstName: String,
/// lastName: String,
/// }
/// };
/// ```
///
/// But you can use rust's `//` and `/*` comment syntax instead:
/// ```rust
/// # use libgraphql::macros::graphql_schema;
/// let schema = graphql_schema! {
/// type Query {
/// me: User,
/// }
///
/// // Represents a user in the system.
/// type User {
/// /* The user's first name. */
/// firstName: String,
///
/// /* The user's last name. */
/// lastName: String,
/// }
/// };
/// ```
///
/// 2) Block-quoted strings (`"""`) *are* supported, but if you need to
/// nest a quoted string /within/ a block-quoted string, you must
/// escape the inner quotes with `\"`. This is because Rust's
/// tokenizer treats unescaped `"` as string delimiters, which
/// breaks the block string recombination.
///
/// So for example, this won't compile:
/// ```rust,compile_fail
/// let schema = graphql_schema! {
/// type Query {
/// me: User,
/// }
///
/// type User {
/// """
/// The user's "primary" address.
/// """
/// address: String,
/// }
/// };
/// ```
///
/// But the workaround is to escape the inner quotes:
/// ```rust
/// # use libgraphql::macros::graphql_schema;
/// let schema = graphql_schema! {
/// type Query {
/// me: User,
/// }
///
/// type User {
/// """
/// The user's \"primary\" address.
/// """
/// address: String,
/// }
/// };
/// ```
/// Evaluates to a [`Schema`](libgraphql_core::schema::Schema) object given a literal
/// Rust `str` containing GraphQL document text that represents a GraphQL
/// schema.
///
/// This macro is effectively a compile-time version of
/// [`SchemaBuilder::build_from_str()`](libgraphql_core::schema::SchemaBuilder::build_from_str()).
///
/// Example usage:
///
/// ```rust
/// use libgraphql::macros::graphql_schema_from_str;
///
/// let schema = graphql_schema_from_str!(r#"
/// type Query {
/// me: User,
///
/// }
///
/// type User {
/// firstName: String,
/// lastName: String,
/// }
/// "#r);
///
/// let user_type =
/// schema.defined_types()
/// .get("User")
/// .unwrap()
/// .as_object()
/// .unwrap();
///
/// assert_eq!(user_type.name(), "User");
/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
/// assert_eq!(user_type.fields().get("doesntExist").is_some(), false);
/// ```