Skip to content

Commit a8362dd

Browse files
authored
fix(api): Fix space filter not working when not adding other filters (#37)
* fix(api): Fix space filter not working when not adding other filters * style: fmt
1 parent 1cf1448 commit a8362dd

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

api/src/schema/query.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ impl Query {
4747
) -> FieldResult<Vec<Entity>> {
4848
let mut query = entity_node::find_many(&executor.context().0);
4949

50-
if let Some(r#where) = r#where {
51-
let filter = entity_node::EntityFilter::from(r#where).with_space_id(&space_id);
52-
53-
query = query.with_filter(filter);
54-
}
50+
let entity_filter = if let Some(r#where) = r#where {
51+
entity_node::EntityFilter::from(r#where).space_id(&space_id)
52+
} else {
53+
entity_node::EntityFilter::default().space_id(&space_id)
54+
};
55+
query = query.with_filter(entity_filter);
5556

5657
match (order_by, order_direction) {
5758
(Some(order_by), Some(OrderDirection::Asc) | None) => {

sdk/src/mapping/entity_node.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ pub struct EntityFilter {
321321
id: Option<PropFilter<String>>,
322322
attributes: Vec<AttributeFilter>,
323323
relations: Option<EntityRelationFilter>,
324+
space_id: Option<PropFilter<String>>,
324325
}
325326

326327
impl EntityFilter {
@@ -354,8 +355,10 @@ impl EntityFilter {
354355

355356
/// Applies a global space_id to all sub-filters (i.e.: attribute and relation filters).
356357
/// If a space_id is already set in a sub-filter, it will be overwritten.
357-
pub fn with_space_id(mut self, space_id: impl Into<String>) -> Self {
358+
pub fn space_id(mut self, space_id: impl Into<String>) -> Self {
358359
let space_id = space_id.into();
360+
self.space_id = Some(prop_filter::value(space_id.clone()));
361+
359362
for attribute in &mut self.attributes {
360363
attribute.space_id_mut(prop_filter::value(&space_id));
361364
}
@@ -375,8 +378,16 @@ impl EntityFilter {
375378
query_part.merge_mut(id.into_query_part(&node_var, "id"));
376379
}
377380

378-
for attribute in self.attributes {
379-
query_part.merge_mut(attribute.into_query_part(&node_var));
381+
if self.attributes.is_empty() {
382+
if let Some(space_id) = self.space_id {
383+
query_part = query_part
384+
.match_clause(format!("({node_var}) -[attribute:ATTRIBUTE]- (:Attribute)",))
385+
.merge(space_id.into_query_part("attribute", "space_id"));
386+
}
387+
} else {
388+
for attribute in self.attributes {
389+
query_part.merge_mut(attribute.into_query_part(&node_var));
390+
}
380391
}
381392

382393
if let Some(relations) = self.relations {

0 commit comments

Comments
 (0)