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

Fix filtering behavior within non-existent @optional blocks. #697

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions trustfall_core/src/interpreter/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn apply_unary_filter<
) -> ContextIterator<'query, Vertex> {
Box::new(iterator.filter_map(move |mut context| {
let last_value = context.values.pop().expect("no value present");
filter_op(&last_value).then_some(context)
(context.within_nonexistent_optional() || filter_op(&last_value)).then_some(context)
}))
}

Expand Down Expand Up @@ -391,7 +391,7 @@ fn apply_filter_op<
left: &FieldValue,
right: &RightValue,
) -> Option<DataContext<Vertex>> {
filter_op(left, right).then_some(ctx)
(ctx.within_nonexistent_optional() || filter_op(left, right)).then_some(ctx)
}

fn apply_filter_with_static_argument_value<'query, Vertex: Debug + Clone + 'query>(
Expand Down
14 changes: 14 additions & 0 deletions trustfall_core/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ impl<Vertex> DataContext<Vertex> {
self.active_vertex.as_ref().and_then(AsVertex::as_vertex)
}

/// Whether this context is currently inside an `@optional` block with no values.
///
/// Let's unpack that:
/// - At this point in the query execution, we're within an `@optional` block of the query.
/// - An `@optional` edge in the evaluation of this context did not exist. It might not be
/// the innermost `@optional` one — it might be a prior one if we're several `@optional` deep.
///
/// This is relevant, for example, for situations where we have filters or type coercions
/// to apply within the `@optional` block and need to know if there's anything to filter/coerce.
#[inline]
pub(crate) fn within_nonexistent_optional(&self) -> bool {
self.active_vertex.is_none()
}

/// Converts `DataContext<Vertex>` to `DataContext<Other>` by mapping each `Vertex` to `Other`.
///
/// If you are implementing an [`Adapter`] for a data source,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Ok(TestParsedGraphQLQuery(
schema_name: "numbers",
query: Query(
root_connection: FieldConnection(
position: Pos(
line: 3,
column: 5,
),
name: "Zero",
),
root_field: FieldNode(
position: Pos(
line: 3,
column: 5,
),
name: "Zero",
connections: [
(FieldConnection(
position: Pos(
line: 4,
column: 9,
),
name: "value",
alias: Some("zero"),
), FieldNode(
position: Pos(
line: 4,
column: 9,
),
name: "value",
alias: Some("zero"),
output: [
OutputDirective(),
],
)),
(FieldConnection(
position: Pos(
line: 6,
column: 9,
),
name: "predecessor",
optional: Some(OptionalDirective()),
), FieldNode(
position: Pos(
line: 6,
column: 9,
),
name: "predecessor",
connections: [
(FieldConnection(
position: Pos(
line: 7,
column: 13,
),
name: "value",
), FieldNode(
position: Pos(
line: 7,
column: 13,
),
name: "value",
filter: [
FilterDirective(
operation: LessThan((), VariableRef("zero")),
),
],
output: [
OutputDirective(),
],
)),
],
)),
],
),
),
arguments: {
"zero": Int64(0),
},
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TestGraphQLQuery (
schema_name: "numbers",
query: r#"
{
Zero {
zero: value @output

predecessor @optional {
value @output @filter(op: "<", value: ["$zero"])
}
}
}"#,
arguments: {
"zero": Int64(0),
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Ok(TestIRQuery(
schema_name: "numbers",
ir_query: IRQuery(
root_name: "Zero",
root_component: IRQueryComponent(
root: Vid(1),
vertices: {
Vid(1): IRVertex(
vid: Vid(1),
type_name: "Number",
),
Vid(2): IRVertex(
vid: Vid(2),
type_name: "Number",
filters: [
LessThan(LocalField(
field_name: "value",
field_type: "Int",
), Variable(VariableRef(
variable_name: "zero",
variable_type: "Int!",
))),
],
),
},
edges: {
Eid(1): IREdge(
eid: Eid(1),
from_vid: Vid(1),
to_vid: Vid(2),
edge_name: "predecessor",
optional: true,
),
},
outputs: {
"value": ContextField(
vertex_id: Vid(2),
field_name: "value",
field_type: "Int",
),
"zero": ContextField(
vertex_id: Vid(1),
field_name: "value",
field_type: "Int",
),
},
),
variables: {
"zero": "Int!",
},
),
arguments: {
"zero": Int64(0),
},
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
TestInterpreterOutputData(
schema_name: "numbers",
outputs: {
"value": Output(
name: "value",
value_type: "Int",
vid: Vid(2),
),
"zero": Output(
name: "zero",
value_type: "Int",
vid: Vid(1),
),
},
results: [
{
"value": Null,
"zero": Int64(0),
},
],
)
Loading
Loading