Skip to content

Commit 8972c3d

Browse files
committed
graphql_query_derive: defer Ident creation
Instead of storing `Ident` structures directly in `FieldType`, create them before macro expansion instead. This allows the `FieldType` to `impl Sync` which then allows for caching the parsing of queries and schemas.
1 parent 8c701b2 commit 8972c3d

File tree

5 files changed

+35
-62
lines changed

5 files changed

+35
-62
lines changed

graphql_query_derive/src/constants.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use deprecation::DeprecationStatus;
22
use field_type::FieldType;
33
use objects::GqlObjectField;
4-
use proc_macro2::{Ident, Span};
54

65
pub(crate) const TYPENAME_FIELD: &str = "__typename";
76

8-
pub(crate) fn string_type() -> Ident {
9-
Ident::new("String", Span::call_site())
7+
pub(crate) fn string_type() -> String {
8+
"String".to_string()
109
}
1110

1211
#[cfg(test)]
13-
pub(crate) fn float_type() -> Ident {
14-
Ident::new("Float", Span::call_site())
12+
pub(crate) fn float_type() -> String {
13+
"Float".to_string()
1514
}
1615

1716
pub(crate) fn typename_field() -> GqlObjectField {

graphql_query_derive/src/field_type.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use schema::DEFAULT_SCALARS;
77

88
#[derive(Clone, Debug, PartialEq, Hash)]
99
pub enum FieldType {
10-
Named(Ident),
10+
Named(String),
1111
Optional(Box<FieldType>),
1212
Vector(Box<FieldType>),
1313
}
@@ -21,27 +21,23 @@ impl FieldType {
2121
prefix.to_string()
2222
};
2323
match &self {
24-
FieldType::Named(name) => {
25-
let name_string = name.to_string();
26-
27-
let name = if context.schema.scalars.contains_key(&name_string) || DEFAULT_SCALARS
24+
FieldType::Named(ref name) => {
25+
let full_name = if context.schema.scalars.contains_key(name) || DEFAULT_SCALARS
2826
.iter()
29-
.any(|elem| elem == &name_string.as_str())
27+
.any(|elem| elem == name)
3028
{
3129
name.clone()
32-
} else if context.schema.enums.contains_key(&name_string) {
33-
Ident::new(
34-
&format!("{}{}", ENUMS_PREFIX, &name_string),
35-
Span::call_site(),
36-
)
30+
} else if context.schema.enums.contains_key(name) {
31+
format!("{}{}", ENUMS_PREFIX, name)
3732
} else {
3833
if prefix.is_empty() {
3934
panic!("Empty prefix for {:?}", self);
4035
}
41-
Ident::new(&prefix, Span::call_site())
36+
prefix
4237
};
38+
let full_name = Ident::new(&full_name, Span::call_site());
4339

44-
quote!(#name)
40+
quote!(#full_name)
4541
}
4642
FieldType::Optional(inner) => {
4743
let inner = inner.to_rust(context, &prefix);
@@ -89,7 +85,7 @@ fn from_schema_type_inner(inner: graphql_parser::schema::Type, non_null: bool) -
8985
}
9086
}
9187
graphql_parser::schema::Type::NamedType(name) => {
92-
let f = FieldType::Named(Ident::new(&name, Span::call_site()));
88+
let f = FieldType::Named(name);
9389
if non_null {
9490
f
9591
} else {
@@ -120,10 +116,7 @@ fn from_json_type_inner(inner: &introspection_response::TypeRef, non_null: bool)
120116
}
121117
}
122118
Some(_) => {
123-
let f = FieldType::Named(Ident::new(
124-
&inner.name.expect("type name"),
125-
Span::call_site(),
126-
));
119+
let f = FieldType::Named(inner.name.expect("type name").clone());
127120
if non_null {
128121
f
129122
} else {
@@ -157,17 +150,14 @@ mod tests {
157150
let ty = GqlParserType::NamedType("Cat".to_string());
158151
assert_eq!(
159152
FieldType::from(ty),
160-
FieldType::Optional(Box::new(FieldType::Named(Ident::new(
161-
"Cat",
162-
Span::call_site()
163-
))))
153+
FieldType::Optional(Box::new(FieldType::Named("Cat".to_string())))
164154
);
165155

166156
let ty = GqlParserType::NonNullType(Box::new(GqlParserType::NamedType("Cat".to_string())));
167157

168158
assert_eq!(
169159
FieldType::from(ty),
170-
FieldType::Named(Ident::new("Cat", Span::call_site()))
160+
FieldType::Named("Cat".to_string())
171161
);
172162
}
173163

@@ -182,10 +172,7 @@ mod tests {
182172
};
183173
assert_eq!(
184174
FieldType::from(ty),
185-
FieldType::Optional(Box::new(FieldType::Named(Ident::new(
186-
"Cat",
187-
Span::call_site()
188-
))))
175+
FieldType::Optional(Box::new(FieldType::Named("Cat".to_string())))
189176
);
190177

191178
let ty = FullTypeFieldsType {
@@ -201,7 +188,7 @@ mod tests {
201188
};
202189
assert_eq!(
203190
FieldType::from(ty),
204-
FieldType::Named(Ident::new("Cat", Span::call_site()))
191+
FieldType::Named("Cat".to_string())
205192
);
206193
}
207194
}

graphql_query_derive/src/inputs.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ mod tests {
116116
GqlObjectField {
117117
description: None,
118118
name: "offsprings".to_string(),
119-
type_: FieldType::Vector(Box::new(FieldType::Named(Ident::new(
120-
"Cat",
121-
Span::call_site(),
122-
)))),
119+
type_: FieldType::Vector(Box::new(FieldType::Named("Cat".to_string()))),
123120
deprecation: DeprecationStatus::Current,
124121
},
125122
),
@@ -128,10 +125,7 @@ mod tests {
128125
GqlObjectField {
129126
description: None,
130127
name: "requirements".to_string(),
131-
type_: FieldType::Optional(Box::new(FieldType::Named(Ident::new(
132-
"CatRequirements",
133-
Span::call_site(),
134-
)))),
128+
type_: FieldType::Optional(Box::new(FieldType::Named("CatRequirements".to_string()))),
135129
deprecation: DeprecationStatus::Current,
136130
},
137131
),

graphql_query_derive/src/schema.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
288288
mod tests {
289289
use super::*;
290290
use constants::*;
291-
use proc_macro2::{Ident, Span};
292291

293292
#[test]
294293
fn build_schema_works() {
@@ -310,47 +309,41 @@ mod tests {
310309
GqlObjectField {
311310
description: None,
312311
name: "id".to_string(),
313-
type_: FieldType::Named(Ident::new("ID", Span::call_site())),
312+
type_: FieldType::Named("ID".to_string()),
314313
deprecation: DeprecationStatus::Current,
315314
},
316315
GqlObjectField {
317316
description: None,
318317
name: "name".to_string(),
319-
type_: FieldType::Named(Ident::new("String", Span::call_site())),
318+
type_: FieldType::Named("String".to_string()),
320319
deprecation: DeprecationStatus::Current,
321320
},
322321
GqlObjectField {
323322
description: None,
324323
name: "friends".to_string(),
325324
type_: FieldType::Optional(Box::new(FieldType::Vector(Box::new(
326-
FieldType::Optional(Box::new(FieldType::Named(Ident::new(
327-
"Character",
328-
Span::call_site(),
329-
)))),
325+
FieldType::Optional(Box::new(FieldType::Named("Character".to_string()))),
330326
)))),
331327
deprecation: DeprecationStatus::Current,
332328
},
333329
GqlObjectField {
334330
description: None,
335331
name: "friendsConnection".to_string(),
336-
type_: FieldType::Named(Ident::new("FriendsConnection", Span::call_site())),
332+
type_: FieldType::Named("FriendsConnection".to_string()),
337333
deprecation: DeprecationStatus::Current,
338334
},
339335
GqlObjectField {
340336
description: None,
341337
name: "appearsIn".to_string(),
342338
type_: FieldType::Vector(Box::new(FieldType::Optional(Box::new(
343-
FieldType::Named(Ident::new("Episode", Span::call_site())),
339+
FieldType::Named("Episode".to_string()),
344340
)))),
345341
deprecation: DeprecationStatus::Current,
346342
},
347343
GqlObjectField {
348344
description: None,
349345
name: "primaryFunction".to_string(),
350-
type_: FieldType::Optional(Box::new(FieldType::Named(Ident::new(
351-
"String",
352-
Span::call_site(),
353-
)))),
346+
type_: FieldType::Optional(Box::new(FieldType::Named("String".to_string()))),
354347
deprecation: DeprecationStatus::Current,
355348
},
356349
],

graphql_query_derive/src/unions.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,20 @@ mod tests {
180180
GqlObjectField {
181181
description: None,
182182
name: "firstName".to_string(),
183-
type_: FieldType::Named(Ident::new("String", Span::call_site())),
183+
type_: FieldType::Named("String".to_string()),
184184
deprecation: DeprecationStatus::Current,
185185
},
186186
GqlObjectField {
187187
description: None,
188188
name: "lastName".to_string(),
189-
type_: FieldType::Named(Ident::new("String", Span::call_site())),
189+
type_: FieldType::Named("String".to_string()),
190190

191191
deprecation: DeprecationStatus::Current,
192192
},
193193
GqlObjectField {
194194
description: None,
195195
name: "createdAt".to_string(),
196-
type_: FieldType::Named(Ident::new("Date", Span::call_site())),
196+
type_: FieldType::Named("Date".to_string()),
197197
deprecation: DeprecationStatus::Current,
198198
},
199199
],
@@ -209,13 +209,13 @@ mod tests {
209209
GqlObjectField {
210210
description: None,
211211
name: "title".to_string(),
212-
type_: FieldType::Named(Ident::new("String", Span::call_site())),
212+
type_: FieldType::Named("String".to_string()),
213213
deprecation: DeprecationStatus::Current,
214214
},
215215
GqlObjectField {
216216
description: None,
217217
name: "created_at".to_string(),
218-
type_: FieldType::Named(Ident::new("Date", Span::call_site())),
218+
type_: FieldType::Named("Date".to_string()),
219219
deprecation: DeprecationStatus::Current,
220220
},
221221
],
@@ -293,7 +293,7 @@ mod tests {
293293
GqlObjectField {
294294
description: None,
295295
name: "createdAt".to_string(),
296-
type_: FieldType::Named(Ident::new("Date", Span::call_site())),
296+
type_: FieldType::Named("Date".to_string()),
297297
deprecation: DeprecationStatus::Current,
298298
},
299299
],
@@ -315,13 +315,13 @@ mod tests {
315315
GqlObjectField {
316316
description: None,
317317
name: "title".to_string(),
318-
type_: FieldType::Named(Ident::new("String", Span::call_site())),
318+
type_: FieldType::Named("String".to_string()),
319319
deprecation: DeprecationStatus::Current,
320320
},
321321
GqlObjectField {
322322
description: None,
323323
name: "createdAt".to_string(),
324-
type_: FieldType::Named(Ident::new("Date", Span::call_site())),
324+
type_: FieldType::Named("Date".to_string()),
325325
deprecation: DeprecationStatus::Current,
326326
},
327327
],

0 commit comments

Comments
 (0)