Skip to content

Commit 1d9d19d

Browse files
committed
graphql: do not sort an interface by child-level entity
1 parent 14ed1d4 commit 1d9d19d

File tree

3 files changed

+75
-34
lines changed

3 files changed

+75
-34
lines changed

graphql/src/schema/api.rs

+16-24
Original file line numberDiff line numberDiff line change
@@ -1053,15 +1053,7 @@ mod tests {
10531053
.map(|value| value.name.as_str())
10541054
.collect();
10551055

1056-
assert_eq!(
1057-
values,
1058-
[
1059-
"id",
1060-
"name",
1061-
"mostHatedBy",
1062-
"mostLovedBy",
1063-
]
1064-
);
1056+
assert_eq!(values, ["id", "name", "mostHatedBy", "mostLovedBy",]);
10651057

10661058
let recipe_order_by = schema
10671059
.get_named_type("Recipe_orderBy")
@@ -1079,21 +1071,21 @@ mod tests {
10791071
.map(|value| value.name.as_str())
10801072
.collect();
10811073

1082-
assert_eq!(
1083-
values,
1084-
[
1085-
"id",
1086-
"name",
1087-
"author",
1088-
"author__id",
1089-
"author__name",
1090-
"author__favoriteFurType",
1091-
"author__favoritePet",
1092-
"author__leastFavoritePet",
1093-
"lovedBy",
1094-
"ingredients"
1095-
]
1096-
);
1074+
assert_eq!(
1075+
values,
1076+
[
1077+
"id",
1078+
"name",
1079+
"author",
1080+
"author__id",
1081+
"author__name",
1082+
"author__favoriteFurType",
1083+
"author__favoritePet",
1084+
"author__leastFavoritePet",
1085+
"lovedBy",
1086+
"ingredients"
1087+
]
1088+
);
10971089
}
10981090

10991091
#[test]

graphql/src/store/query.rs

+7
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,13 @@ fn build_order_by(
451451
})
452452
}
453453
OrderByValue::Child(parent_field_name, child_field_name) => {
454+
if entity.is_interface() {
455+
return Err(QueryExecutionError::OrderByNotSupportedError(
456+
entity.name().to_owned(),
457+
parent_field_name.clone(),
458+
));
459+
}
460+
454461
let field =
455462
sast::get_field(entity, parent_field_name.as_str()).ok_or_else(|| {
456463
QueryExecutionError::EntityFieldError(

graphql/tests/query.rs

+52-10
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,21 @@ fn test_schema(id: DeploymentHash, id_type: IdType) -> Schema {
213213
id: ID!
214214
title: String!
215215
song: Song!
216+
author: User!
216217
}
217218
218219
type Photo implements Media @entity {
219220
id: ID!
220221
title: String!
221222
song: Song! @derivedFrom(field: \"media\")
223+
author: User!
222224
}
223225
224226
type Video implements Media @entity {
225227
id: ID!
226228
title: String!
227229
song: Song! @derivedFrom(field: \"media\")
230+
author: User!
228231
}
229232
230233
interface Release {
@@ -261,6 +264,7 @@ fn test_schema(id: DeploymentHash, id_type: IdType) -> Schema {
261264
latestSongReview: SongReview!
262265
latestBandReview: BandReview!
263266
latestReview: Review!
267+
medias: [Media!]! @derivedFrom(field: \"author\")
264268
}
265269
266270
type AnonymousUser implements Author @entity {
@@ -315,12 +319,12 @@ async fn insert_test_entities(
315319
entity! { __typename: "User", id: "u1", name: "Baden", latestSongReview: "r3", latestBandReview: "r1", latestReview: "r1" },
316320
entity! { __typename: "User", id: "u2", name: "Goodwill", latestSongReview: "r4", latestBandReview: "r2", latestReview: "r2" },
317321
entity! { __typename: "AnonymousUser", id: "u3", name: "Anonymous 3", latestSongReview: "r6", latestBandReview: "r5", latestReview: "r5" },
318-
entity! { __typename: "Photo", id: md[1], title: "Cheesy Tune Single Cover" },
319-
entity! { __typename: "Video", id: md[2], title: "Cheesy Tune Music Video" },
320-
entity! { __typename: "Photo", id: md[3], title: "Rock Tune Single Cover" },
321-
entity! { __typename: "Video", id: md[4], title: "Rock Tune Music Video" },
322-
entity! { __typename: "Photo", id: md[5], title: "Pop Tune Single Cover" },
323-
entity! { __typename: "Video", id: md[6], title: "Folk Tune Music Video" },
322+
entity! { __typename: "Photo", id: md[1], title: "Cheesy Tune Single Cover", author: "u1" },
323+
entity! { __typename: "Video", id: md[2], title: "Cheesy Tune Music Video", author: "u2" },
324+
entity! { __typename: "Photo", id: md[3], title: "Rock Tune Single Cover", author: "u1" },
325+
entity! { __typename: "Video", id: md[4], title: "Rock Tune Music Video", author: "u2" },
326+
entity! { __typename: "Photo", id: md[5], title: "Pop Tune Single Cover", author: "u1" },
327+
entity! { __typename: "Video", id: md[6], title: "Folk Tune Music Video", author: "u2" },
324328
entity! { __typename: "Album", id: "rl1", title: "Pop and Folk", songs: vec![s[3], s[4]] },
325329
entity! { __typename: "Single", id: "rl2", title: "Rock", songs: vec![s[2]] },
326330
entity! { __typename: "Single", id: "rl3", title: "Cheesy", songs: vec![s[1]] },
@@ -806,13 +810,51 @@ fn can_query_with_sorting_by_child_interface() {
806810
}
807811

808812
#[test]
809-
fn can_query_interface_with_sorting_by_child_entity() {
810-
// TODO
813+
fn can_not_query_interface_with_sorting_by_child_entity() {
814+
const QUERY: &str = "
815+
query {
816+
desc: medias(first: 100, orderBy: author__name, orderDirection: desc) {
817+
title
818+
author {
819+
name
820+
}
821+
}
822+
asc: medias(first: 100, orderBy: author__name, orderDirection: asc) {
823+
title
824+
author {
825+
name
826+
}
827+
}
828+
}";
829+
830+
run_query(QUERY, |result, _| {
831+
// Sorting an interface by child-level entity (derived) is not supported
832+
assert!(result.has_errors());
833+
});
811834
}
812835

813836
#[test]
814-
fn can_query_interface_with_sorting_by_derived_child_entity() {
815-
// TODO
837+
fn can_not_query_interface_with_sorting_by_derived_child_entity() {
838+
const QUERY: &str = "
839+
query {
840+
desc: medias(first: 100, orderBy: song__title, orderDirection: desc) {
841+
title
842+
song {
843+
title
844+
}
845+
}
846+
asc: medias(first: 100, orderBy: song__title, orderDirection: asc) {
847+
title
848+
song {
849+
title
850+
}
851+
}
852+
}";
853+
854+
run_query(QUERY, |result, _| {
855+
// Sorting an interface by child-level entity is not supported
856+
assert!(result.has_errors());
857+
});
816858
}
817859

818860
#[test]

0 commit comments

Comments
 (0)