Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an iterator over all the required properties of a Vertex #449

Merged
merged 22 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion trustfall_core/src/interpreter/hints/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,11 @@ fn recurse_then_nested_required_edge_depth_two() {
}

mod static_property_values {
use std::collections::HashSet;
use std::ops::Bound;

use crate::{
interpreter::hints::{CandidateValue, Range},
interpreter::hints::{vertex_info::RequiredProperty, CandidateValue, Range},
ir::FieldValue,
};

Expand Down Expand Up @@ -580,6 +581,31 @@ mod static_property_values {
assert_eq!(adapter.on_starting_vertices.borrow()[&vid(1)].calls, 1);
}

#[test]
era marked this conversation as resolved.
Show resolved Hide resolved
fn required_properties_test() {
let input_name = "required_properties";

let adapter = TestAdapter {
on_starting_vertices: btreemap! {
vid(1) => TrackCalls::<ResolveInfoFn>::new_underlying(Box::new(|info| {
assert!(info.coerced_to_type().is_none());
assert_eq!(vid(1), info.vid());

assert_eq!(hashset![
RequiredProperty::new("name".into()),
RequiredProperty::new("value".into()),
RequiredProperty::new("__typename".into())
], info.required_properties().collect::<HashSet<RequiredProperty>>());
era marked this conversation as resolved.
Show resolved Hide resolved
})),
}
.into(),
..Default::default()
};

let adapter = run_query(adapter, input_name);
assert_eq!(adapter.on_starting_vertices.borrow()[&vid(1)].calls, 1);
}

#[test]
fn typename_filter() {
let input_name = "typename_filter";
Expand Down
55 changes: 55 additions & 0 deletions trustfall_core/src/interpreter/hints/vertex_info.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::{
collections::BTreeMap,
ops::{Bound, RangeBounds},
Expand All @@ -14,6 +15,18 @@ use crate::{

use super::{dynamic::DynamicallyResolvedValue, CandidateValue, EdgeInfo, Range};

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RequiredProperty {
era marked this conversation as resolved.
Show resolved Hide resolved
pub name: Arc<str>,
era marked this conversation as resolved.
Show resolved Hide resolved
}

impl RequiredProperty {
pub fn new(name: Arc<str>) -> Self {
Self { name }
}
}

/// Information about what the currently-executing query needs at a specific vertex.
#[cfg_attr(docsrs, doc(notable_trait))]
pub trait VertexInfo: super::sealed::__Sealed {
Expand All @@ -23,6 +36,8 @@ pub trait VertexInfo: super::sealed::__Sealed {
/// The type coercion (`... on SomeType`) applied by the query at this vertex, if any.
fn coerced_to_type(&self) -> Option<&Arc<str>>;

fn required_properties(&self) -> Box<dyn Iterator<Item = RequiredProperty> + '_>;

/// Check whether the query demands this vertex property to have specific values:
/// a single value, or one of a set or range of values. The candidate values
/// are known *statically*: up-front, without executing any of the query.
Expand Down Expand Up @@ -132,6 +147,46 @@ impl<T: InternalVertexInfo + super::sealed::__Sealed> VertexInfo for T {
}
}

/// required_properties returns an iterator over all properties needed
era marked this conversation as resolved.
Show resolved Hide resolved
/// for this vertex.
fn required_properties(&self) -> Box<dyn Iterator<Item = RequiredProperty> + '_> {
let current_component = self.current_component();
era marked this conversation as resolved.
Show resolved Hide resolved

let current_vertex = self.current_vertex();

let properties = current_component
.outputs
.values()
.filter(|c| c.vertex_id == current_vertex.vid)
.map(|c| RequiredProperty::new(c.field_name.clone()));

let properties = properties.chain(
current_vertex
.filters
.iter()
.map(|f| RequiredProperty::new(f.left().field_name.clone())),
);

let properties = properties.chain(current_component.vertices.values().flat_map(|v| {
v.filters
.iter()
.filter_map(|f| match f.right() {
Some(Argument::Tag(FieldRef::ContextField(ctx))) => {
if current_vertex.vid == ctx.vertex_id {
Some(ctx.field_name.clone())
} else {
None
}
}
_ => None,
})
.map(RequiredProperty::new)
}));

let mut seen_property = HashSet::new();
Box::new(properties.filter(move |r| seen_property.insert(r.name.clone())))
}

fn statically_required_property(&self, property: &str) -> Option<CandidateValue<&FieldValue>> {
if self.non_binding_filters() {
// This `VertexInfo` is in a place where the filters applied to fields
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
Ok(TestParsedGraphQLQuery(
schema_name: "numbers",
query: Query(
root_connection: FieldConnection(
position: Pos(
line: 3,
column: 5,
),
name: "Number",
arguments: {
"max": Int64(2),
"min": Int64(1),
},
),
root_field: FieldNode(
position: Pos(
line: 3,
column: 5,
),
name: "Number",
connections: [
(FieldConnection(
position: Pos(
line: 4,
column: 9,
),
name: "__typename",
), FieldNode(
position: Pos(
line: 4,
column: 9,
),
name: "__typename",
filter: [
FilterDirective(
operation: Equals((), VariableRef("type")),
),
],
)),
(FieldConnection(
position: Pos(
line: 5,
column: 9,
),
name: "value",
), FieldNode(
position: Pos(
line: 5,
column: 9,
),
name: "value",
output: [
OutputDirective(),
],
)),
(FieldConnection(
position: Pos(
line: 6,
column: 9,
),
name: "name",
), FieldNode(
position: Pos(
line: 6,
column: 9,
),
name: "name",
tag: [
TagDirective(),
],
)),
(FieldConnection(
position: Pos(
line: 7,
column: 9,
),
name: "predecessor",
fold: Some(FoldGroup(
fold: FoldDirective(),
)),
), FieldNode(
position: Pos(
line: 7,
column: 9,
),
name: "predecessor",
connections: [
(FieldConnection(
position: Pos(
line: 8,
column: 13,
),
name: "name",
), FieldNode(
position: Pos(
line: 8,
column: 13,
),
name: "name",
output: [
OutputDirective(),
],
)),
],
)),
(FieldConnection(
position: Pos(
line: 10,
column: 9,
),
name: "multiple",
arguments: {
"max": Int64(5),
},
), FieldNode(
position: Pos(
line: 10,
column: 9,
),
name: "multiple",
connections: [
(FieldConnection(
position: Pos(
line: 11,
column: 13,
),
name: "name",
), FieldNode(
position: Pos(
line: 11,
column: 13,
),
name: "name",
filter: [
FilterDirective(
operation: Equals((), TagRef("name")),
),
],
)),
],
)),
],
),
),
arguments: {
"type": String("Prime"),
},
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
TestGraphQLQuery (
schema_name: "numbers",
query: r#"
{
Number(min: 1, max: 2) {
__typename @filter(op: "=", value: ["$type"])
value @output
name @tag
predecessor @fold {
name @output
}
multiple(max: 5) {
name @filter(op: "=", value: ["%name"])
}
}
}"#,
arguments: {
"type": String("Prime"),
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Ok(TestIRQuery(
schema_name: "numbers",
ir_query: IRQuery(
root_name: "Number",
root_parameters: EdgeParameters(
contents: {
"max": Int64(2),
"min": Int64(1),
},
),
root_component: IRQueryComponent(
root: Vid(1),
vertices: {
Vid(1): IRVertex(
vid: Vid(1),
type_name: "Number",
filters: [
Equals(LocalField(
field_name: "__typename",
field_type: "String!",
), Variable(VariableRef(
variable_name: "type",
variable_type: "String!",
))),
],
),
Vid(3): IRVertex(
vid: Vid(3),
type_name: "Composite",
filters: [
Equals(LocalField(
field_name: "name",
field_type: "String",
), Tag(ContextField(ContextField(
vertex_id: Vid(1),
field_name: "name",
field_type: "String",
)))),
],
),
},
edges: {
Eid(2): IREdge(
eid: Eid(2),
from_vid: Vid(1),
to_vid: Vid(3),
edge_name: "multiple",
parameters: EdgeParameters(
contents: {
"max": Int64(5),
},
),
),
},
folds: {
Eid(1): IRFold(
eid: Eid(1),
from_vid: Vid(1),
to_vid: Vid(2),
edge_name: "predecessor",
component: IRQueryComponent(
root: Vid(2),
vertices: {
Vid(2): IRVertex(
vid: Vid(2),
type_name: "Number",
),
},
outputs: {
"name": ContextField(
vertex_id: Vid(2),
field_name: "name",
field_type: "String",
),
},
),
),
},
outputs: {
"value": ContextField(
vertex_id: Vid(1),
field_name: "value",
field_type: "Int",
),
},
),
variables: {
"type": "String!",
},
),
arguments: {
"type": String("Prime"),
},
))
Loading
Loading