Skip to content

Commit

Permalink
wip: orderBy on relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Druue committed Jan 10, 2024
1 parent d467ef7 commit 271eb8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ mod distinct {
{ title }
}
}"),
Postgres(_) => r###"{"data":{"findManyUser":[{"id":1,"first_name":"Joe","posts":[]},{"id":4,"first_name":"Papa","posts":[{"title":"1"}]},{"id":3,"first_name":"Rocky","posts":[]},{"id":5,"first_name":"Troll","posts":[{"title":"2"},{"title":"3"}]}]}}"###,
Postgres(_) => r###"{"data":{"findManyUser":[{"id":1,"first_name":"Joe","posts":[{"title":"1"},{"title":"2"},{"title":"3"}]},{"id":4,"first_name":"Papa","posts":[{"title":"1"}]},{"id":3,"first_name":"Rocky","posts":[]},{"id":5,"first_name":"Troll","posts":[{"title":"2"},{"title":"3"}]}]}}"###,
_ => r###"{"data":{"findManyUser":[{"id":1,"first_name":"Joe","posts":[{"title":"1"},{"title":"2"},{"title":"3"}]},{"id":4,"first_name":"Papa","posts":[{"title":"1"}]},{"id":3,"first_name":"Rocky","posts":[]},{"id":5,"first_name":"Troll","posts":[{"title":"2"},{"title":"3"}]}]}}"###
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
query_document::{ParsedArgument, ParsedInputMap},
QueryGraphBuilderError, QueryGraphBuilderResult,
};

use itertools::Itertools;
use query_structure::{prelude::*, QueryArguments};
use schema::constants::{aggregations, args, ordering};
use std::convert::TryInto;
Expand Down Expand Up @@ -328,6 +330,7 @@ fn extract_compound_cursor_field(

/// Runs final transformations on the QueryArguments.
fn finalize_arguments(mut args: QueryArguments, model: &Model) -> QueryArguments {
dbg!(&args);
// Check if the query requires an implicit ordering added to the arguments.
// An implicit ordering is convenient for deterministic results for take and skip, for cursor it's _required_
// as a cursor needs a direction to page. We simply take the primary identifier as a default order-by.
Expand All @@ -346,5 +349,34 @@ fn finalize_arguments(mut args: QueryArguments, model: &Model) -> QueryArguments
args.order_by.extend(order_bys);
}

args
// TODO(@druue): define when we need to do this
let add_relation_id = args.model.name() != "User";

let distinct = if add_relation_id {
let fs = if let Some(distinct) = &args.distinct {
// TODO(@druue): figure out how to only get the specific relation ids that are relevant for the query
// TODO(@druue): as this currently just pulls all of them
// ?(@druue): Is there a cleaner / more succinct way to do this?
let field_ids = args
.model()
.fields()
.relation()
.map(|r| r.linking_fields())
.collect_vec();

let fs = dbg!(query_structure::FieldSelection::union(field_ids));

Some(distinct.clone().merge(fs))
} else {
None
};

fs
} else {
args.distinct.clone()
};

dbg!(&distinct);

QueryArguments { distinct, ..args }
}

0 comments on commit 271eb8f

Please sign in to comment.