Skip to content

Commit 26e10cc

Browse files
author
Alexei Pastuchov
committed
Implement ApplyOffset for diesel::mysql::Mysq
1 parent 927deca commit 26e10cc

File tree

1 file changed

+32
-4
lines changed
  • wundergraph/src/query_builder/selection

1 file changed

+32
-4
lines changed

wundergraph/src/query_builder/selection/offset.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use crate::error::Result;
2-
#[cfg(any(feature = "postgres", feature = "sqlite"))]
2+
#[cfg(any(feature = "postgres", feature = "sqlite", feature = "mysql"))]
33
use crate::error::WundergraphError;
4-
#[cfg(any(feature = "postgres", feature = "sqlite"))]
4+
#[cfg(any(feature = "postgres", feature = "sqlite", feature = "mysql"))]
55
use crate::juniper_ext::FromLookAheadValue;
66
use crate::query_builder::selection::{BoxedQuery, LoadingHandler};
77
use crate::scalar::WundergraphScalarValue;
88
use diesel::backend::Backend;
9-
#[cfg(feature = "sqlite")]
9+
#[cfg(any(feature = "sqlite", feature = "mysql"))]
1010
use diesel::query_dsl::methods::LimitDsl;
11-
#[cfg(any(feature = "postgres", feature = "sqlite"))]
11+
#[cfg(any(feature = "postgres", feature = "sqlite", feature = "mysql"))]
1212
use diesel::query_dsl::methods::OffsetDsl;
13+
1314
use juniper::LookAheadSelection;
1415

1516
/// A trait abstracting over the different behaviour of limit/offset
@@ -72,3 +73,30 @@ impl ApplyOffset for diesel::sqlite::Sqlite {
7273
}
7374
}
7475
}
76+
77+
#[cfg(feature = "mysql")]
78+
impl ApplyOffset for diesel::mysql::Mysql {
79+
fn apply_offset<'a, L, Ctx>(
80+
query: BoxedQuery<'a, L, Self, Ctx>,
81+
select: &LookAheadSelection<'_, WundergraphScalarValue>,
82+
) -> Result<BoxedQuery<'a, L, Self, Ctx>>
83+
where
84+
L: LoadingHandler<Self, Ctx>,
85+
{
86+
use juniper::LookAheadMethods;
87+
if let Some(offset) = select.argument("offset") {
88+
let q = <_ as OffsetDsl>::offset(
89+
query,
90+
i64::from_look_ahead(offset.value())
91+
.ok_or(WundergraphError::CouldNotBuildFilterArgument)?,
92+
);
93+
if select.argument("limit").is_some() {
94+
Ok(q)
95+
} else {
96+
Ok(<_ as LimitDsl>::limit(q, std::i64::MAX))
97+
}
98+
} else {
99+
Ok(query)
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)