#[macro_export]
macro_rules! define_composite_query {
(
$( #[$($struct_meta:tt)*] )*
$vis:vis struct $name:ident {
$(
$( #[$($field_meta:tt)*] )*
$field_fn:ident : $builder_ty:path
),* $(,)?
}
) => {
$( #[$($struct_meta)*] )*
#[derive(Debug, Default, Clone)]
$vis struct $name {
$(
$( #[$($field_meta)*] )*
pub(crate) $field_fn: Option<$builder_ty>,
)*
pub(crate) max_complexity_override: Option<u32>,
pub(crate) max_depth_override: Option<u32>,
pub(crate) use_variables: bool,
pub(crate) operation_name: Option<String>,
pub(crate) custom_variables: Option<serde_json::Value>,
}
impl $name {
pub fn new() -> Self {
Self::default()
}
pub fn use_variables(&mut self, enable: bool) -> &mut Self {
self.use_variables = enable;
self
}
$(
$( #[$($field_meta)*] )*
pub fn $field_fn<F>(&mut self, builder_fn: F) -> &mut Self
where
F: FnOnce($builder_ty) -> $builder_ty,
{
let builder = self.$field_fn.take().unwrap_or_default();
self.$field_fn = Some(builder_fn(builder));
self
}
)*
pub fn with_operation_name(&mut self, name: impl Into<String>) -> &mut Self {
self.operation_name = Some(name.into());
self
}
pub fn with_variables(&mut self, variables: serde_json::Value) -> &mut Self {
self.custom_variables = Some(variables);
self
}
pub fn operation_name(&self) -> Option<&str> {
self.operation_name.as_deref()
}
pub fn variables(&self) -> Option<&serde_json::Value> {
self.custom_variables.as_ref()
}
pub fn with_max_complexity(&mut self, max: u32) -> &mut Self {
self.max_complexity_override = Some(max);
self
}
pub fn with_max_depth(&mut self, max: u32) -> &mut Self {
self.max_depth_override = Some(max);
self
}
pub fn max_complexity(&self) -> Option<u32> {
if let Some(max) = self.max_complexity_override {
return Some(max);
}
$(
if let Some(ref b) = self.$field_fn {
if let Some(max) = $crate::traits::GqlInspectable::max_complexity(b) {
return Some(max);
}
}
)*
None
}
pub fn max_depth(&self) -> Option<u32> {
if let Some(max) = self.max_depth_override {
return Some(max);
}
$(
if let Some(ref b) = self.$field_fn {
if let Some(max) = $crate::traits::GqlInspectable::max_depth(b) {
return Some(max);
}
}
)*
None
}
pub fn complexity(&self) -> u32 {
let mut total = 0;
$(
if let Some(ref b) = self.$field_fn {
total += $crate::traits::GqlInspectable::complexity(b);
}
)*
total
}
pub fn depth(&self) -> u32 {
let mut max_depth = 1;
$(
if let Some(ref b) = self.$field_fn {
max_depth = max_depth.max($crate::traits::GqlInspectable::depth(b));
}
)*
max_depth
}
pub fn build_payload_details(
&self,
) -> (
Vec<String>,
Vec<String>,
serde_json::Map<String, serde_json::Value>,
) {
let field_count = [$( stringify!($field_fn) ),*].len();
let mut root_queries = Vec::with_capacity(field_count);
let mut all_decls = Vec::with_capacity(field_count * 2);
let mut all_vars = serde_json::Map::with_capacity(field_count * 2);
$(
if let Some(ref b) = self.$field_fn {
let (q_part, decls, vars) = $crate::traits::BuildableQuery::_build_query_parts(b, self.use_variables);
root_queries.push(q_part);
all_decls.extend(decls);
all_vars.extend(vars);
}
)*
if let Some(custom) = &self.custom_variables {
if let Some(obj) = custom.as_object() {
for (k, v) in obj {
all_vars.insert(k.clone(), v.clone());
}
}
}
(root_queries, all_decls, all_vars)
}
pub fn build_payload_parts(&self) -> (String, Option<serde_json::Value>) {
let (root_queries, all_decls, all_vars) = self.build_payload_details();
let final_query = {
let has_decls = self.use_variables && !all_decls.is_empty();
let prefix_and_wrap_len = if has_decls { 14 } else { 10 };
let decls_len: usize = if has_decls {
all_decls.iter().map(|d| d.len()).sum::<usize>()
+ (all_decls.len().saturating_sub(1) * 2) } else {
0
};
let roots_len: usize = root_queries.iter().map(|r| r.len()).sum::<usize>()
+ root_queries.len().saturating_sub(1);
let mut doc = String::with_capacity(prefix_and_wrap_len + decls_len + roots_len);
if has_decls {
doc.push_str("query (");
for (i, decl) in all_decls.iter().enumerate() {
if i > 0 {
doc.push_str(", ");
}
doc.push_str(decl);
}
doc.push_str(") { ");
} else {
doc.push_str("query { ");
}
for (i, root) in root_queries.iter().enumerate() {
if i > 0 {
doc.push(' ');
}
doc.push_str(root);
}
doc.push_str(" }");
doc
};
let final_vars = if all_vars.is_empty() {
None
} else {
Some(serde_json::Value::Object(all_vars))
};
(final_query, final_vars)
}
pub fn build_query(&self) -> String {
let (q, _) = self.build_payload_parts();
q
}
#[doc(hidden)]
pub fn _build_payload_parts(&self) -> (String, Option<serde_json::Value>) {
self.build_payload_parts()
}
#[doc(hidden)]
pub fn _build_query(&self) -> String {
self.build_query()
}
}
impl $crate::traits::GqlInspectable for $name {
fn complexity(&self) -> u32 {
self.complexity()
}
fn depth(&self) -> u32 {
self.depth()
}
fn max_complexity(&self) -> Option<u32> {
self.max_complexity()
}
fn max_depth(&self) -> Option<u32> {
self.max_depth()
}
}
impl $crate::traits::GqlValidatable for $name {
fn validate_args(&self) -> $crate::error::Result<()> {
$(
if let Some(ref b) = self.$field_fn {
$crate::traits::GqlValidatable::validate_args(b)?;
}
)*
Ok(())
}
}
impl $crate::traits::CompositeQueryDocument for $name {
fn use_variables(&mut self, enable: bool) -> &mut Self {
self.use_variables(enable)
}
fn build_payload_details(
&self,
) -> (
Vec<String>,
Vec<String>,
serde_json::Map<String, serde_json::Value>,
) {
self.build_payload_details()
}
}
};
}