use serde::{Deserialize, Serialize};
use vantage_vista::{NoExtras, VistaSpec};
use crate::graphql::api::CountMode;
use crate::graphql::condition::FilterDialect;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct GraphqlTableExtras {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub graphql: Option<GraphqlBlock>,
}
impl GraphqlTableExtras {
pub fn new(block: GraphqlBlock) -> Self {
Self {
graphql: Some(block),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct GraphqlBlock {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub root_field: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub count_field: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dialect: Option<FilterDialect>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub filter_arg: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub args: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub response_path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub filter: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub order: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub search: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub paginate: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub count: Option<CountMode>,
}
impl GraphqlBlock {
pub fn with_root_field(mut self, root_field: impl Into<String>) -> Self {
self.root_field = Some(root_field.into());
self
}
pub fn with_count_field(mut self, count_field: impl Into<String>) -> Self {
self.count_field = Some(count_field.into());
self
}
pub fn with_dialect(mut self, dialect: FilterDialect) -> Self {
self.dialect = Some(dialect);
self
}
pub fn with_filter_arg(mut self, filter_arg: impl Into<String>) -> Self {
self.filter_arg = Some(filter_arg.into());
self
}
pub fn with_args(mut self, args: serde_json::Value) -> Self {
self.args = Some(args);
self
}
pub fn with_response_path(mut self, response_path: impl Into<String>) -> Self {
self.response_path = Some(response_path.into());
self
}
pub fn with_filter(mut self, on: bool) -> Self {
self.filter = Some(on);
self
}
pub fn with_order(mut self, on: bool) -> Self {
self.order = Some(on);
self
}
pub fn with_search(mut self, on: bool) -> Self {
self.search = Some(on);
self
}
pub fn with_paginate(mut self, on: bool) -> Self {
self.paginate = Some(on);
self
}
pub fn with_count(mut self, count: CountMode) -> Self {
self.count = Some(count);
self
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct GraphqlColumnExtras {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub graphql: Option<GraphqlColumnBlock>,
}
impl GraphqlColumnExtras {
pub fn new(block: GraphqlColumnBlock) -> Self {
Self {
graphql: Some(block),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct GraphqlColumnBlock {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub field: Option<String>,
}
impl GraphqlColumnBlock {
pub fn with_field(mut self, field: impl Into<String>) -> Self {
self.field = Some(field.into());
self
}
}
pub type GraphqlApiVistaSpec = VistaSpec<GraphqlTableExtras, GraphqlColumnExtras, NoExtras>;