Skip to content

Commit d786ffa

Browse files
committed
(codegen) Move GraphQLTYpeDefinition to utils
1 parent a53c60b commit d786ffa

File tree

2 files changed

+152
-151
lines changed

2 files changed

+152
-151
lines changed

juniper_codegen/src/impl_object.rs

Lines changed: 3 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -126,154 +126,6 @@ impl FieldAttributes {
126126
}
127127
}
128128

129-
pub struct GraphQLTypeDefinitionFieldArg {
130-
pub name: String,
131-
pub description: Option<String>,
132-
pub _type: syn::Type,
133-
}
134-
135-
pub struct GraphQLTypeDefinitionField {
136-
pub name: String,
137-
pub _type: syn::Type,
138-
pub description: Option<String>,
139-
pub deprecation: Option<String>,
140-
pub args: Vec<GraphQLTypeDefinitionFieldArg>,
141-
pub resolver_code: proc_macro2::TokenStream,
142-
}
143-
144-
/// Definition of a graphql type based on information extracted
145-
/// by various macros.
146-
/// The definition can be rendered to Rust code.
147-
pub struct GraphQLTypeDefiniton {
148-
pub name: String,
149-
pub _type: syn::Type,
150-
pub context: Option<syn::Type>,
151-
pub scalar: Option<syn::Type>,
152-
pub description: Option<String>,
153-
pub fields: Vec<GraphQLTypeDefinitionField>,
154-
}
155-
156-
impl GraphQLTypeDefiniton {
157-
fn to_tokens(&self) -> proc_macro2::TokenStream {
158-
let name = &self.name;
159-
let ty = &self._type;
160-
let context = self
161-
.context
162-
.as_ref()
163-
.map(|ctx| quote!( #ctx ))
164-
.unwrap_or(quote!(()));
165-
let scalar = self
166-
.scalar
167-
.as_ref()
168-
.map(|s| quote!( #s ))
169-
.unwrap_or(quote!(juniper::DefaultScalarValue));
170-
171-
let field_definitions = self.fields.iter().map(|field| {
172-
let args = field.args.iter().map(|arg| {
173-
let arg_type = &arg._type;
174-
let arg_name = &arg.name;
175-
quote! {
176-
.argument(registry.arg::<#arg_type>(#arg_name, info))
177-
}
178-
});
179-
180-
let description = match field.description.as_ref() {
181-
Some(description) => quote!( .description(#description) ),
182-
None => quote!(),
183-
};
184-
185-
let deprecation = match field.deprecation.as_ref() {
186-
Some(deprecation) => quote!( .deprecation(#deprecation) ),
187-
None => quote!(),
188-
};
189-
190-
let field_name = &field.name;
191-
192-
let _type = &field._type;
193-
quote! {
194-
registry
195-
.field_convert::<#_type, _, Self::Context>(#field_name, info)
196-
#(#args)*
197-
#description
198-
#deprecation
199-
}
200-
});
201-
202-
let resolve_matches = self.fields.iter().map(|field| {
203-
let name = &field.name;
204-
let code = &field.resolver_code;
205-
206-
quote!(
207-
#name => {
208-
let res = #code;
209-
juniper::IntoResolvable::into(
210-
res,
211-
executor.context()
212-
)
213-
.and_then(|res| {
214-
match res {
215-
Some((ctx, r)) => executor.replaced_context(ctx).resolve_with_ctx(&(), &r),
216-
None => Ok(juniper::Value::null()),
217-
}
218-
})
219-
},
220-
)
221-
});
222-
223-
let description = match self.description.as_ref() {
224-
Some(description) => quote!( .description(#description) ),
225-
None => quote!(),
226-
};
227-
228-
quote!(
229-
impl juniper::GraphQLType<#scalar> for #ty {
230-
type Context = #context;
231-
type TypeInfo = ();
232-
233-
fn name(_: &Self::TypeInfo) -> Option<&str> {
234-
Some(#name)
235-
}
236-
237-
fn meta<'r>(
238-
info: &Self::TypeInfo,
239-
registry: &mut juniper::Registry<'r, #scalar>
240-
) -> juniper::meta::MetaType<'r, #scalar>
241-
where #scalar : 'r,
242-
for<'z> &'z juniper::DefaultScalarValue: juniper::ScalarRefValue<'z>,
243-
{
244-
let fields = vec![
245-
#( #field_definitions ),*
246-
];
247-
let meta = registry.build_object_type::<#ty>( info, &fields );
248-
let meta = meta # description;
249-
meta.into_meta()
250-
}
251-
252-
#[allow(unused_variables)]
253-
#[allow(unused_mut)]
254-
fn resolve_field(
255-
&self,
256-
_info: &(),
257-
field: &str,
258-
args: &juniper::Arguments,
259-
executor: &juniper::Executor<Self::Context>,
260-
) -> juniper::ExecutionResult {
261-
match field {
262-
#( #resolve_matches )*
263-
_ => {
264-
panic!("Field {} not found on type {}", field, "Mutation");
265-
}
266-
}
267-
}
268-
269-
fn concrete_type_name(&self, _: &Self::Context, _: &Self::TypeInfo) -> String {
270-
#name.to_string()
271-
}
272-
273-
}
274-
)
275-
}
276-
}
277129

278130
/// Generate code for the juniper::impl_object macro.
279131
pub fn build(args: TokenStream, body: TokenStream) -> TokenStream {
@@ -309,7 +161,7 @@ pub fn build(args: TokenStream, body: TokenStream) -> TokenStream {
309161

310162
let target_type = *_impl.self_ty.clone();
311163

312-
let mut definition = GraphQLTypeDefiniton {
164+
let mut definition = util::GraphQLTypeDefiniton {
313165
name,
314166
_type: target_type.clone(),
315167
context: impl_attrs.context,
@@ -386,7 +238,7 @@ pub fn build(args: TokenStream, body: TokenStream) -> TokenStream {
386238
.get::<#ty>(#arg_name)
387239
.expect("Argument missing - validation must have failed")
388240
));
389-
args.push(GraphQLTypeDefinitionFieldArg {
241+
args.push(util::GraphQLTypeDefinitionFieldArg {
390242
name: arg_name,
391243
description: None,
392244
_type: ty.clone(),
@@ -407,7 +259,7 @@ pub fn build(args: TokenStream, body: TokenStream) -> TokenStream {
407259
.unwrap_or(util::to_camel_case(&method_ident.to_string()));
408260
let description = attrs.description.or(util::get_doc_comment(&method.attrs));
409261

410-
definition.fields.push(GraphQLTypeDefinitionField {
262+
definition.fields.push(util::GraphQLTypeDefinitionField {
411263
name,
412264
_type,
413265
args,

juniper_codegen/src/util.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,155 @@ pub fn is_valid_name(field_name: &str) -> bool {
248248
GRAPHQL_NAME_SPEC.is_match(field_name)
249249
}
250250

251+
pub struct GraphQLTypeDefinitionFieldArg {
252+
pub name: String,
253+
pub description: Option<String>,
254+
pub _type: syn::Type,
255+
}
256+
257+
pub struct GraphQLTypeDefinitionField {
258+
pub name: String,
259+
pub _type: syn::Type,
260+
pub description: Option<String>,
261+
pub deprecation: Option<String>,
262+
pub args: Vec<GraphQLTypeDefinitionFieldArg>,
263+
pub resolver_code: proc_macro2::TokenStream,
264+
}
265+
266+
/// Definition of a graphql type based on information extracted
267+
/// by various macros.
268+
/// The definition can be rendered to Rust code.
269+
pub struct GraphQLTypeDefiniton {
270+
pub name: String,
271+
pub _type: syn::Type,
272+
pub context: Option<syn::Type>,
273+
pub scalar: Option<syn::Type>,
274+
pub description: Option<String>,
275+
pub fields: Vec<GraphQLTypeDefinitionField>,
276+
}
277+
278+
impl GraphQLTypeDefiniton {
279+
pub fn to_tokens(&self) -> proc_macro2::TokenStream {
280+
let name = &self.name;
281+
let ty = &self._type;
282+
let context = self
283+
.context
284+
.as_ref()
285+
.map(|ctx| quote!( #ctx ))
286+
.unwrap_or(quote!(()));
287+
let scalar = self
288+
.scalar
289+
.as_ref()
290+
.map(|s| quote!( #s ))
291+
.unwrap_or(quote!(juniper::DefaultScalarValue));
292+
293+
let field_definitions = self.fields.iter().map(|field| {
294+
let args = field.args.iter().map(|arg| {
295+
let arg_type = &arg._type;
296+
let arg_name = &arg.name;
297+
quote! {
298+
.argument(registry.arg::<#arg_type>(#arg_name, info))
299+
}
300+
});
301+
302+
let description = match field.description.as_ref() {
303+
Some(description) => quote!( .description(#description) ),
304+
None => quote!(),
305+
};
306+
307+
let deprecation = match field.deprecation.as_ref() {
308+
Some(deprecation) => quote!( .deprecation(#deprecation) ),
309+
None => quote!(),
310+
};
311+
312+
let field_name = &field.name;
313+
314+
let _type = &field._type;
315+
quote! {
316+
registry
317+
.field_convert::<#_type, _, Self::Context>(#field_name, info)
318+
#(#args)*
319+
#description
320+
#deprecation
321+
}
322+
});
323+
324+
let resolve_matches = self.fields.iter().map(|field| {
325+
let name = &field.name;
326+
let code = &field.resolver_code;
327+
328+
quote!(
329+
#name => {
330+
let res = #code;
331+
juniper::IntoResolvable::into(
332+
res,
333+
executor.context()
334+
)
335+
.and_then(|res| {
336+
match res {
337+
Some((ctx, r)) => executor.replaced_context(ctx).resolve_with_ctx(&(), &r),
338+
None => Ok(juniper::Value::null()),
339+
}
340+
})
341+
},
342+
)
343+
});
344+
345+
let description = match self.description.as_ref() {
346+
Some(description) => quote!( .description(#description) ),
347+
None => quote!(),
348+
};
349+
350+
quote!(
351+
impl juniper::GraphQLType<#scalar> for #ty {
352+
type Context = #context;
353+
type TypeInfo = ();
354+
355+
fn name(_: &Self::TypeInfo) -> Option<&str> {
356+
Some(#name)
357+
}
358+
359+
fn meta<'r>(
360+
info: &Self::TypeInfo,
361+
registry: &mut juniper::Registry<'r, #scalar>
362+
) -> juniper::meta::MetaType<'r, #scalar>
363+
where #scalar : 'r,
364+
for<'z> &'z juniper::DefaultScalarValue: juniper::ScalarRefValue<'z>,
365+
{
366+
let fields = vec![
367+
#( #field_definitions ),*
368+
];
369+
let meta = registry.build_object_type::<#ty>( info, &fields );
370+
let meta = meta # description;
371+
meta.into_meta()
372+
}
373+
374+
#[allow(unused_variables)]
375+
#[allow(unused_mut)]
376+
fn resolve_field(
377+
&self,
378+
_info: &(),
379+
field: &str,
380+
args: &juniper::Arguments,
381+
executor: &juniper::Executor<Self::Context>,
382+
) -> juniper::ExecutionResult {
383+
match field {
384+
#( #resolve_matches )*
385+
_ => {
386+
panic!("Field {} not found on type {}", field, "Mutation");
387+
}
388+
}
389+
}
390+
391+
fn concrete_type_name(&self, _: &Self::Context, _: &Self::TypeInfo) -> String {
392+
#name.to_string()
393+
}
394+
395+
}
396+
)
397+
}
398+
}
399+
251400
#[cfg(test)]
252401
mod test {
253402
use super::*;

0 commit comments

Comments
 (0)