Search code examples
rustrust-diesel

Weird error when inserting a value with diesel and juniper


I am currently making a small backend with actix-web, juniper and diesel. It is mostly based on this tutorial : https://dev.to/youroff/ultra-fast-backend-with-rust-26ac but I did some changes.

I finished the "read" part of the API, but I have an error on the "create" part.

impl MutationRoot {
    fn create_contraption(
        context: &Context,
        new_contraption: ContraptionInput,
    ) -> FieldResult<Contraption> {
        use crate::schema::contraptions;

        let conn = context.dbpool.get().map_err(|_| {
            FieldError::new("Could not open connection to the database", Value::null())
        })?;

        diesel::insert_into(contraptions::table)
            .values(&new_contraption)
            .get_result(&conn)
            .expect("Could not create new contraptions")
    }
}

Here is the ouput of cargo check

    Checking redstone_contraptions_backend v0.1.0 (/home/stowy/Documents/dev/rust/Redstone-Contraptions/redstone_contraptions_backend)
error[E0277]: the trait bound `Result<Contraption, FieldError>: Queryable<(Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::sql_types::Nullable<diesel::sql_types::Text>), Pg>` is not satisfied
  --> src/models/root.rs:46:14
   |
46 |             .get_result(&conn)
   |              ^^^^^^^^^^ the trait `Queryable<(Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::sql_types::Nullable<diesel::sql_types::Text>), Pg>` is not implemented for `Result<Contraption, FieldError>`
   |
   = note: required because of the requirements on the impl of `LoadQuery<PooledConnection<ConnectionManager<PgConnection>>, Result<Contraption, FieldError>>` for `InsertStatement<table, ValuesClause<(ColumnInsertValue<columns::name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>, ColumnInsertValue<columns::description, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>, ColumnInsertValue<columns::image, diesel::expression::bound::Bound<diesel::sql_types::Nullable<diesel::sql_types::Text>, &std::string::String>>, ColumnInsertValue<columns::itemslist, diesel::expression::bound::Bound<diesel::sql_types::Nullable<diesel::sql_types::Text>, &std::string::String>>), table>>`

From what I understand, it says that the output of .get_result(&conn) which is of type Result<Contraption, FieldError> doesn't implement Queryable. But it's normal since it's a Result type and not Contraption, that's why I put an .expect() afterwards.

And Contraption implements Queryable :

#[derive(Default, Queryable)]
pub struct Contraption {
    pub id: i32,
    pub name: String,
    pub description: String,
    pub image: Option<String>,
    pub itemslist: Option<String>,
}

#[derive(GraphQLInputObject, Insertable)]
#[graphql(description = "Contraption Input")]
#[table_name = "contraptions"]
pub struct ContraptionInput {
    pub name: String,
    pub description: String,
    pub image: Option<String>,
    pub itemslist: Option<String>,
}

Here are my dependencies just in case :

[dependencies]
actix = "0.12.0"
actix-web = "3.3.2"
juniper = "0.15.7"
diesel = { version = "1.4.7", features = ["postgres", "r2d2"] }
dotenv = "0.15.0"
r2d2 = "0.8.9"
serde_json = "1.0.67"

What am I doing wrong ?


Solution

  • I just needed to add a .map_err() instead of the .expect() and precise the type of the output of .get_result().

    diesel::insert_into(contraptions::table)
        .values(&new_contraption)
        .get_result::<Contraption>(&conn)
        .map_err(|_| FieldError::new("Error creating contraption", Value::null()))