Skip to content

Commit

Permalink
feat: rely on automatic dependency patching instead of manually const…
Browse files Browse the repository at this point in the history
…ructing selection set
  • Loading branch information
laplab committed Jan 11, 2024
1 parent bafa048 commit 76f7ca2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
6 changes: 0 additions & 6 deletions query-engine/core/src/executor/execute_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,6 @@ async fn execute_on<'a>(
fn build_graph(query_schema: &QuerySchema, operation: Operation) -> crate::Result<(QueryGraph, IrSerializer<'_>)> {
let (query_graph, serializer) = QueryGraphBuilder::new(query_schema).build(operation)?;

// TODO laplab: Remove.
// static COUNTER: std::sync::Mutex<u32> = std::sync::Mutex::new(0);
// *COUNTER.lock().unwrap() += 1;
// let current = *COUNTER.lock().unwrap();
// std::fs::write(format!("graph{}.graphviz", current), query_graph.to_graphviz()).unwrap();

Ok((query_graph, serializer))
}

Expand Down
14 changes: 9 additions & 5 deletions query-engine/core/src/query_ast/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ impl WriteQuery {
/// Updates the field selection of the query to satisfy the inputted FieldSelection.
pub fn satisfy_dependency(&mut self, fields: FieldSelection) {
match self {
Self::CreateRecord(cr) => cr.selected_fields = cr.selected_fields.clone().merge(fields),
Self::UpdateRecord(UpdateRecord::WithSelection(ur)) => {
ur.selected_fields = ur.selected_fields.clone().merge(fields)
}
Self::CreateRecord(cr) => cr.selected_fields.merge_in_place(fields),
Self::UpdateRecord(UpdateRecord::WithSelection(ur)) => ur.selected_fields.merge_in_place(fields),
Self::UpdateRecord(UpdateRecord::WithoutSelection(_)) => (),
Self::CreateManyRecords(_) => (),
Self::DeleteRecord(_) => (),
Self::DeleteRecord(DeleteRecord {
selected_fields: Some(selected_fields),
..
}) => selected_fields.fields.merge_in_place(fields),
Self::DeleteRecord(DeleteRecord {
selected_fields: None, ..
}) => (),
Self::UpdateManyRecords(_) => (),
Self::DeleteManyRecords(_) => (),
Self::ConnectRecords(_) => (),
Expand Down
5 changes: 3 additions & 2 deletions query-engine/core/src/query_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,9 @@ impl QueryGraph {
continue;
}

// No connector supports returning more than the primary identifier for a delete just yet.
if query.is_delete_one() {
// If the connector does not support returning more than the primary identifier for a delete,
// do not update the selection set.
if query.is_delete_one() && !capabilities.contains(ConnectorCapability::DeleteReturning) {
continue;
}

Expand Down
15 changes: 1 addition & 14 deletions query-engine/core/src/query_graph_builder/write/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,9 @@ pub(crate) fn delete_record(
if can_use_atomic_delete(query_schema, &field, &filter) {
// Database supports returning the deleted row, so just the delete node will suffice.
let nested_fields = field.nested_fields.unwrap().fields;
let mut selected_fields = read::utils::collect_selected_scalars(&nested_fields, &model);
let selected_fields = read::utils::collect_selected_scalars(&nested_fields, &model);
let selection_order = read::utils::collect_selection_order(&nested_fields);

// Make sure to request all foreign key fields in addition to the fields selected by the user.
// This ensures that delete emulation below has all the data it needs, so that the "reload" node
// is not inserted between delete node and emulation subtree.
// NOTE: "Reload" node will always throw an error here because we just deleted the record
// it tries to fetch.
let internal_model = &model.dm;
let relation_fields = internal_model.fields_pointing_to_model(&model);
for relation_field in relation_fields {
let parent_relation_field = relation_field.related_field();
let linking_fields = parent_relation_field.linking_fields();
selected_fields = selected_fields.merge(linking_fields);
}

let delete_query = Query::Write(WriteQuery::DeleteRecord(DeleteRecord {
name: field.name,
model: model.clone(),
Expand Down
5 changes: 5 additions & 0 deletions query-engine/query-structure/src/field_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ impl FieldSelection {
FieldSelection { selections }
}

pub fn merge_in_place(&mut self, other: FieldSelection) {
let this = std::mem::take(self);
*self = this.merge(other);
}

pub fn type_identifiers_with_arities(&self) -> Vec<(TypeIdentifier, FieldArity)> {
self.selections()
.filter_map(|selection| match selection {
Expand Down

0 comments on commit 76f7ca2

Please sign in to comment.