iwilsonq / rust-graphql-example

The accompanying project for a tutorial. Uses Rust, Juniper GraphQL Server + Actix, and Diesel ORM.
119 stars 14 forks source link

Use r2d2 pool for database connections ? #1

Open Farkal opened 5 years ago

Farkal commented 5 years ago

Hey, first thank you very mush for your great example ! There is not a lot of good examples with actix, juniper and diesel (and up to date).

So my question is why you didn't manage database connection with r2d2 ? It would be great to see some examples using the Context (because i don't really see how i can use it to pass my database pool or also the app state (I manage files cache in hashmap stored in the state))

iwilsonq commented 5 years ago

Thank you! That would be a great improvement, I could take a look at updating the example this week. I didn't at first because I think I was running into difficulty and was more focused on releasing quickly, but I do think a db pool in context would be the right solution :)

Farkal commented 5 years ago

I have done it myself yesterday in my own app (it was pretty easy). I find example in the comment of your great article -> https://github.com/fattenap/actix-web-juniper-diesel. But it's not a complete example and I find myself how to do nested with context (for example with your Team struct) (on the juniper doc it's with the old syntax: graphql_object!):

#[juniper::object(Context = Context, description = "A team of members")]
impl Team {
  pub fn id(&self) -> i32 {
    self.id
  }

  pub fn name(&self) -> &str {
    self.name.as_str()
  }

  pub fn members(&self, context: &Context) -> Vec<Member> {
    use crate::schema::members::dsl::*;
    let connection = context.db.get().unwrap();
    members
      .filter(team_id.eq(self.id))
      .limit(100)
      .load::<Member>(&connection)
      .expect("Error loading members")
  }
}
iwilsonq commented 5 years ago

I updated the code according to your example, it should be more complete now 💯

Farkal commented 5 years ago

Great ! Do you think you can add some tests ? I don't find the best way to test my app (how to create integration/unit tests for graphql (is there some useful libs))