Repository: p2panda/aquadoggo (language: rust)

Issue:
Set order default of collection query to document id
As per specification.

We want this only for root collection queries, not for nested relation list queries, because there, by default, we want to order by list index.


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bba6a7413..dbafe0021 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Removed replication service for now, preparing for new replication protocol [#296](https://github.com/p2panda/aquadoggo/pull/296)
 - Bring back e2e GraphQL API tests [#342](https://github.com/p2panda/aquadoggo/pull/342)
 - Move GraphQL `types` into separate modules [#343](https://github.com/p2panda/aquadoggo/pull/343)
+- Set default order for root queries to document id [#352](https://github.com/p2panda/aquadoggo/pull/352)
 - Remove property tests again because of concurrency bug [#347](https://github.com/p2panda/aquadoggo/pull/347)
 
 ### Fixed
diff --git a/aquadoggo/src/db/query/order.rs b/aquadoggo/src/db/query/order.rs
index 61938e7d6..1f4cf4181 100644
--- a/aquadoggo/src/db/query/order.rs
+++ b/aquadoggo/src/db/query/order.rs
@@ -40,22 +40,3 @@ impl Default for Order {
         }
     }
 }
-
-#[cfg(test)]
-mod tests {
-    use super::{Direction, Order};
-
-    #[test]
-    fn default_specification() {
-        // If no ordering is selected the documents will be ordered by document id, ascending
-        let order = Order::default();
-
-        assert_eq!(
-            order,
-            Order {
-                field: None,
-                direction: Direction::Ascending
-            }
-        )
-    }
-}
diff --git a/aquadoggo/src/graphql/resolvers.rs b/aquadoggo/src/graphql/resolvers.rs
index 124aa2ea8..7ec45414a 100644
--- a/aquadoggo/src/graphql/resolvers.rs
+++ b/aquadoggo/src/graphql/resolvers.rs
@@ -73,7 +73,7 @@ pub async fn resolve_document_collection(
     let store = ctx.data_unchecked::<SqlStore>();
 
     // Populate query arguments with values from GraphQL query
-    let query = parse_collection_arguments(&ctx, &schema)?;
+    let query = parse_collection_arguments(&ctx, &schema, &list)?;
 
     // Fetch all queried documents and compose the value to be passed up the query tree
     let (pagination_data, documents) = store.query(&schema, &query, list.as_ref()).await?;
diff --git a/aquadoggo/src/graphql/utils.rs b/aquadoggo/src/graphql/utils.rs
index 5cba5ed68..25142cebb 100644
--- a/aquadoggo/src/graphql/utils.rs
+++ b/aquadoggo/src/graphql/utils.rs
@@ -14,7 +14,7 @@ use p2panda_rs::storage_provider::traits::DocumentStore;
 use crate::db::query::{
     Direction, Field, Filter, MetaField, Order, Pagination, PaginationField, Select,
 };
-use crate::db::stores::{PaginationCursor, Query};
+use crate::db::stores::{PaginationCursor, Query, RelationList};
 use crate::db::types::StorageDocument;
 use crate::db::SqlStore;
 use crate::graphql::constants;
@@ -120,6 +120,7 @@ pub fn filter_to_operation_value(
 pub fn parse_collection_arguments(
     ctx: &ResolverContext,
     schema: &Schema,
+    list: &Option<RelationList>,
 ) -> Result<Query<PaginationCursor>, Error> {
     let mut pagination = Pagination::<PaginationCursor>::default();
     let mut order = Order::default();
@@ -172,6 +173,11 @@ pub fn parse_collection_arguments(
     let select = Select::new(fields.as_slice());
     pagination.fields = pagination_fields;
 
+    // Set default ordering to document id as per specification, if we're in a root query
+    if list.is_none() && order.field.is_none() {
+        order.field = Some(Field::Meta(MetaField::DocumentId));
+    }
+
     // Finally put it all together
     let query = Query::new(&pagination, &select, &filter, &order);
 


Test patch: