Skip to content

Commit

Permalink
refactor: move dedupe logic to transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Oct 5, 2024
1 parent ed4dea2 commit 32e116c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
13 changes: 1 addition & 12 deletions src/core/jit/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,25 +422,14 @@ impl<Input> OperationPlan<Input> {
.map(|f| f.into_nested(&fields))
.collect::<Vec<_>>();

let dedupe = fields
.iter()
.map(|field| {
if let Some(IR::IO(io)) = field.ir.as_ref() {
io.dedupe()
} else {
true
}
})
.all(|a| a);

Self {
root_name: root_name.to_string(),
flat: fields,
nested,
operation_type,
index,
is_introspection_query,
dedupe,
dedupe: false,
is_const: false,
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/jit/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::Deserialize;

use super::{transform, Builder, OperationPlan, Result, Variables};
use crate::core::blueprint::Blueprint;
use crate::core::transform::TransformerOps;
use crate::core::valid::Validator;
use crate::core::Transform;

Expand Down Expand Up @@ -44,7 +45,8 @@ impl Request<ConstValue> {
let builder = Builder::new(blueprint, doc);
let plan = builder.build(self.operation_name.as_deref())?;

let plan = transform::ConstCheck::new()
let plan = transform::CheckConst::new()
.pipe(transform::CheckDedupe::new())
.transform(plan.clone())
.to_result()
.unwrap_or(plan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::core::jit::OperationPlan;
use crate::core::valid::Valid;
use crate::core::Transform;

pub struct ConstCheck<A>(PhantomData<A>);
impl<A> ConstCheck<A> {
pub struct CheckConst<A>(PhantomData<A>);
impl<A> CheckConst<A> {
pub fn new() -> Self {
Self(PhantomData)
}
}

impl<A> Transform for ConstCheck<A> {
impl<A> Transform for CheckConst<A> {
type Value = OperationPlan<A>;
type Error = ();

Expand Down
31 changes: 31 additions & 0 deletions src/core/jit/transform/check_dedupe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::core::{ir::model::IR, jit::OperationPlan, valid::Valid, Transform};

pub struct CheckDedupe<A>(std::marker::PhantomData<A>);
impl<A> CheckDedupe<A> {
pub fn new() -> Self {
Self(std::marker::PhantomData)
}
}

impl<A> Transform for CheckDedupe<A> {
type Value = OperationPlan<A>;
type Error = ();

fn transform(&self, mut plan: Self::Value) -> Valid<Self::Value, Self::Error> {
let dedupe = plan
.flat
.iter()
.map(|field| {
if let Some(IR::IO(io)) = field.ir.as_ref() {
io.dedupe()
} else {
true
}
})
.all(|a| a);

plan.dedupe = dedupe;

Valid::succeed(plan)
}
}
6 changes: 4 additions & 2 deletions src/core/jit/transform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod const_check;
mod check_const;
mod check_dedupe;
mod input_resolver;
mod skip;

pub use const_check::*;
pub use check_const::*;
pub use check_dedupe::*;
pub use input_resolver::*;
pub use skip::*;

0 comments on commit 32e116c

Please sign in to comment.