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 3 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
27 changes: 26 additions & 1 deletion trustfall_core/src/interpreter/hints/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ mod static_property_values {
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 +580,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!(vec![RequiredProperty::new("value".into()),
RequiredProperty::new("multiple".into()),
era marked this conversation as resolved.
Show resolved Hide resolved
RequiredProperty::new("predecessor".into()),
RequiredProperty::new("__typename".into())
], info.required_properties());
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
64 changes: 64 additions & 0 deletions trustfall_core/src/interpreter/hints/vertex_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ use crate::{

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

#[derive(Debug, Clone, PartialEq, Eq)]
era marked this conversation as resolved.
Show resolved Hide resolved
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 +34,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) -> Vec<RequiredProperty>;
era marked this conversation as resolved.
Show resolved Hide resolved

/// 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 +145,57 @@ 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) -> Vec<RequiredProperty> {
let current_component = self.current_component();
era marked this conversation as resolved.
Show resolved Hide resolved

let current_vertex = self.current_vertex();

// we need all output properties
let mut properties: Vec<RequiredProperty> = current_component
.outputs
.values()
.map(|c| RequiredProperty::new(c.field_name.clone()))
.collect::<Vec<RequiredProperty>>();

// extend with the edges from this vertex
properties.extend(
current_component
.edges
.values()
.map(|a| RequiredProperty::new(a.edge_name.clone()))
.collect::<Vec<RequiredProperty>>(),
);

properties.extend(
current_component
.folds
.values()
.map(|i| RequiredProperty::new(i.edge_name.clone()))
.collect::<Vec<RequiredProperty>>(),
);
era marked this conversation as resolved.
Show resolved Hide resolved

// properties.extend(
era marked this conversation as resolved.
Show resolved Hide resolved
// current_component
// .vertices
// .values()
// .filter(|v| v.vid != current_vertex.vid)
// .map(|v| RequiredProperty::new(v.type_name.clone()))
// .collect::<Vec<RequiredProperty>>(),
// );

properties.extend(
current_vertex
.filters
.iter()
.map(|f| RequiredProperty::new(f.left().field_name.clone()))
.collect::<Vec<RequiredProperty>>(),
);

properties
}

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,23 @@
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"])
}

}


}"#,
era marked this conversation as resolved.
Show resolved Hide resolved
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