Skip to content

Commit

Permalink
Add optimization to VertexType entrypoint in schema adapter (#339)
Browse files Browse the repository at this point in the history
* it works!

* No more rc

* WIP multiple candidates code

* small fix

* Defeating the BORROW CHECKER

* use expect instead of unwrap in tests

* fix the other unwrap

* dont pluralize singular variable

* use brackets and partial_cmp in tests

* Update trustfall_core/src/schema/adapter/tests.rs

Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>

* fix tests

* Add use for candidate value

* replace .filter .map with .and_then

---------

Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>
  • Loading branch information
u9g and obi1kenobi authored Jul 12, 2023
1 parent d30fec9 commit 2e2c7c9
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 6 deletions.
60 changes: 54 additions & 6 deletions trustfall_core/src/schema/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::{
accessor_property, field_property,
interpreter::{
helpers::{resolve_neighbors_with, resolve_property_with},
ContextIterator, ContextOutcomeIterator, ResolveEdgeInfo, ResolveInfo, VertexIterator,
CandidateValue, ContextIterator, ContextOutcomeIterator, ResolveEdgeInfo, ResolveInfo,
VertexInfo, VertexIterator,
},
ir::{types::get_base_named_type, EdgeParameters, FieldValue},
};
Expand Down Expand Up @@ -214,15 +215,62 @@ impl<'a> crate::interpreter::Adapter<'a> for SchemaAdapter<'a> {
&self,
edge_name: &Arc<str>,
_parameters: &EdgeParameters,
_resolve_info: &ResolveInfo,
resolve_info: &ResolveInfo,
) -> VertexIterator<'a, Self::Vertex> {
let candidate_value = resolve_info.statically_required_property("name").clone();
match edge_name.as_ref() {
"VertexType" => {
let root_query_type = self.schema.query_type_name();
Box::new(self.schema.vertex_types.values().filter_map(move |v| {
(v.name.node != root_query_type)
.then(|| SchemaVertex::VertexType(VertexType::new(v)))
}))

if let Some(CandidateValue::Single(FieldValue::String(name_wanted))) =
candidate_value
{
let name_wanted = name_wanted.as_str();
if let Some(exact_wanted) = self
.schema
.vertex_types
.get(name_wanted)
.filter(move |v| v.name.node != root_query_type)
{
Box::new(std::iter::once(SchemaVertex::VertexType(VertexType::new(
exact_wanted,
))))
} else {
Box::new(std::iter::empty())
}
} else if let Some(CandidateValue::Multiple(possibilities)) = candidate_value {
let possibilities_as_owned_strings = possibilities
.iter()
.map(|el| {
el.as_str()
.expect("for name possibility to be a string")
.to_string()
})
.collect::<Vec<_>>();

let vertex_types = &self.schema.vertex_types;

Box::new(
possibilities_as_owned_strings
.into_iter()
.filter_map(move |wanted| {
vertex_types.get(wanted.as_str()).and_then(|exact_wanted| {
if exact_wanted.name.node != root_query_type {
Some(SchemaVertex::VertexType(VertexType::new(
exact_wanted,
)))
} else {
None
}
})
}),
)
} else {
Box::new(self.schema.vertex_types.values().filter_map(move |v| {
(v.name.node != root_query_type)
.then(|| SchemaVertex::VertexType(VertexType::new(v)))
}))
}
}
"Entrypoint" => Box::new(Box::new(
self.schema
Expand Down
51 changes: 51 additions & 0 deletions trustfall_core/src/schema/adapter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,57 @@ fn check_vertex_type_properties() {
assert_eq!(expected_rows, rows);
}

#[test]
fn check_vertex_type_properties_using_one_of() {
let query = r#"
{
VertexType {
name @filter(op: "one_of", value: ["$name"]) @output
property {
property: name @output
}
}
}"#;
let args = btreemap! {
"name".into() => vec!["VertexType", "Property"].into(),
}
.into();
let adapter = Arc::new(SchemaAdapter::new(&SCHEMA));

let indexed = crate::frontend::parse(&SCHEMA, query).expect("not a valid query");
let mut rows: Vec<_> = crate::interpreter::execution::interpret_ir(adapter, indexed, args)
.expect("execution error")
.collect();

rows.sort_by(|a, b| {
a["property"]
.partial_cmp(&b["property"])
.expect("to be comparable")
});

let expected_rows = vec![
btreemap! {
"property".into() => "is_interface".into(),
"name".into() => "VertexType".into()
},
btreemap! {
"property".into() => "name".into(),
"name".into() => "VertexType".into()
},
btreemap! {
"property".into() => "name".into(),
"name".into() => "Property".into()
},
btreemap! {
"property".into() => "type".into(),
"name".into() => "Property".into()
},
];

assert_eq!(expected_rows, rows);
}

#[test]
fn check_entrypoint_target_edges() {
let query = r#"
Expand Down

0 comments on commit 2e2c7c9

Please sign in to comment.