#[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 mut root_queries = Vec::new();
let mut all_decls = Vec::new();
let mut all_vars = serde_json::Map::new();
$(
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);
for (k, v) in vars {
all_vars.insert(k, v);
}
}
)*
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 query_body = if root_queries.is_empty() {
String::new()
} else {
root_queries.join(" ")
};
let final_query = if self.use_variables && !all_decls.is_empty() {
format!("query ({}) {{ {} }}", all_decls.join(", "), query_body)
} else {
format!("query {{ {} }}", query_body)
};
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()
}
}
};
}