Graphql. can I make this work


I have this schema

table! {
    members (id) {
        id -> Int4,
        name -> Varchar,
        knockouts -> Int4,
        team_id -> Int4,

table! {
    teams (id) {
        id -> Int4,
        name -> Varchar,

joinable!(members -> teams (team_id));


now I wonder if I enter a team name or a teamid can I with graphql make a list of all members of a team

I have already this :

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

  pub fn name(&self) -> &str {

  pub fn members(&self) -> Vec<Member> {
    use crate::schema::members::dsl::*;
    let connection = establish_connection(); 
       .expect("Error loading members")

I'm not sure what the actual Question is here.
Teams have members, and so With graphql it's normal for a Team to have a member field which will return a list of members. I don't know which specific Database or storage you are using but in general your approach is correct. You can define the graphql fields as methods in the Impl block of the type, and you can have children like members be retrieved in the method.

One change I would recommend is, instead of reconnecting in the members, you should Connect only once when the app starts and use it as the context.

Here's some snippets from a small forum like thing I'm making as a hobby project with mongodb


//Have a static client here
static CLIENT: Lazy<db::Client> = Lazy::new(db::connect);

There's probably a better way to pass along the client instead of making it static, I'm also just hacking it together for now. Will make it better after everything works. Regardless, Connection is done only once.


//Implement Context for Client, so it can be used as Context
impl juniper::Context for Client {}

async fn graphql(
    st: web::Data<Arc<Schema>>,
    data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> {
    let res = data.execute(&st, &*CLIENT); //Passing the client here 
    let result = serde_json::to_string(&res)?;


//notice the 'Context', it's the same Type passed in at
#[juniper::object(Context = Client)]
impl Article {
    fn id(&self) -> &str {
    fn title(&self) -> &str {
    fn content(&self) -> &str {
    fn imgurl(&self) -> &str {
    fn comments(&self, ctx: &Client) -> Vec<Comment> {
                           //^^^^^^ You can use it here 
        let collection = ctx.db.collection("comments");
        let comments = collection
                doc! {"articleid": ObjectId::with_string("invalid id")},
            ), None)
            .expect("fetching comments failed");

        let comments: Vec<Comment> = comments
            .map(|maybe_document| {
                let comment = maybe_document.unwrap();


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.