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

Revise the mutations API #501

Merged
merged 5 commits into from
Jun 24, 2024
Merged

Revise the mutations API #501

merged 5 commits into from
Jun 24, 2024

Conversation

soupi
Copy link
Contributor

@soupi soupi commented Jun 19, 2024

What

We want to make the api for mutations pretty. We make the following changes:

  1. Rename arguments
  2. prefix the argument names of unique constraint columns with key_

How

General design

  • We generate delete, insert and update procedures for each table.
  • A single insert procedure is generated per table of the form:
    <mutation-version>_insert_<table>(
        objects: [<object>],
        post_check: <boolexpr>
    )
    It lets us insert multiple objects and include a post check for permissions.
  • A delete procedure is generated per table X unique constraint of the form:
    <mutation-version>_delete_<table>_by_<column_and_column_and...>(
        key_<column1>: <value>,
        key_<column2>: <value>,
        ...,
        pre_check: <boolexpr>
    )
    It lets us delete a single row using the uniqueness constraint, and contains a pre check for permissions.
  • An update procedure is generated per table X unique constraint of the form:
    <mutation-version>_update_<table>_by_<column_and_column_and...>(
        key_<column1>: <value>,
        key_<column2>: <value>,
        ...,
        update_columns: { <column>: { _set: <value> }, ... },
        pre_check: <boolexpr>,
        post_check: <boolexpr>
    )
    It lets us update a single row using the uniqueness constraint by updating the relevant columns, and contains a pre check and post check for permissions.
    Note that the update_columns structure is different than the v2 version where we had _set, _inc, and other fields.
  • Mutations using uniqueness constraints use the naming schema by_column_and_column_and_column instead of the db constraint name, because the former is far more helpful.
  • If generating a mutation encounters an internal error, we skip that particular mutation instead of throwing an error so the connector can start at any situation.
  • Naming collisions between the unique constraints and the update_columns / pre_check / post_check is avoided by prefixing argument names of the columns of a unique constraint with key_.

@@ -20,15 +22,9 @@ pub struct InsertMutation {
pub description: String,
pub schema_name: sql::ast::SchemaName,
pub table_name: sql::ast::TableName,
pub objects_argument_name: String,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we can specify the name once instead of having magic strings.

@soupi soupi marked this pull request as ready for review June 21, 2024 07:20
crates/connectors/ndc-postgres/src/schema.rs Outdated Show resolved Hide resolved
crates/connectors/ndc-postgres/src/schema.rs Outdated Show resolved Hide resolved
@@ -119,7 +115,7 @@ pub fn translate(
.iter()
.map(|by_column| {
let unique_key = arguments
.get(&by_column.name)
.get(&format!("{}{}", columns_prefix, by_column.name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the arguments map should use a tuple or something as a key?

Copy link
Contributor Author

@soupi soupi Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arguments comes from ndc-spec, and they are not used only for keys, but also for update_columns, pre_check, etc.

I agree that this isn't looking great, but I'm not sure there's a nicer solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, makes sense.

.get(&filter.argument_name)
.ok_or(Error::ArgumentNotFound(filter.argument_name.clone()))?;
.get(&pre_check.argument_name)
.ok_or(Error::ArgumentNotFound(pre_check.argument_name.clone()))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.ok_or(Error::ArgumentNotFound(pre_check.argument_name.clone()))?;
.ok_or_else(|| Error::ArgumentNotFound(pre_check.argument_name.clone()))?;

"array of values structure in insert _objects argument. Expecting an array of objects."
.to_string(),
)),
serde_json::Value::Array(_) => Err(Error::UnexpectedStructure(format!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this in the Display impl of UnexpectedStructure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment made sense to me. I went to have a look what would I need to parameterize UnexpectedStructure by to make this work, and the result was:

  • array of arrays
  • insert
  • {} argument
  • an array of objects

Which is essentially almost the entire message :( Might not be worth trying to parameterize in that case, and we probably don't need a dedicated error time for each unexpected structure.

.get("_objects")
.ok_or(Error::ArgumentNotFound("_objects".to_string()))?;
.get(&mutation.objects_argument_name)
.ok_or(Error::ArgumentNotFound(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.ok_or(Error::ArgumentNotFound(
.ok_or_else(|| Error::ArgumentNotFound(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went to have a look. It seems like .ok_or_else is useful when the or_else part is an expensive computation. This isn't the case here - we need to create a closure that returns this particular data, I think this will end up doing more work and require more memory, won't it? So isn't it strictly worse than just ok_or in this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just because of the .clone(); I think it's probably borderline.

Co-authored-by: Samir Talwar <samir.talwar@hasura.io>
@soupi soupi added this pull request to the merge queue Jun 24, 2024
Merged via the queue into main with commit 097a4e9 Jun 24, 2024
35 checks passed
@soupi soupi deleted the gil/mutations-interface branch June 24, 2024 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants