Mutation with many-to-many relation failing with "connect"

prisma

#1

Hi all, first post - thanks ahead of time to anyone that helps me out.

I’m fiddling around with Prisma - hosted prisma server on Heroku, and am using Node.js and the graphql-yoga package to write a server implementation. Where I’m having trouble is with a mutation that links User records in a many-to-many fashion to Club records. Here’s my datamodel.prisma that I prisma deploy to the service:

type Club {
    id: ID! @unique
    createdAt: DateTime!
    name: String!
    description: String!
    members: [User!] @relation(name: "ClubMembers")
}

type User {
    id: ID! @unique
    firstName: String!
    lastName: String!
    email: String! @unique
    clubs: [Club!] @relation(name: "ClubMembers")
    password: String!
}

My Mutation (Node.js) Code

I am using the following mutation to link an existing Club to an existing User. It takes 2 parameters, the clubid (Club ID) and the id (User ID).

  async function addUserToClub(parent, args, context, info) {
      console.log('connecting user with id: ' + args.id);
      console.log('to: ' + args.clubid);
      const club = await context.prisma.updateClub({
          data: {
              members: {
                  connect: {
                    id: args.id
                  }
              }
          },
          where: {
              id: args.clubid
          }
      })

      return club;
  }

What is interesting, is if I use the GraphQL Playground directly on the prisma server using the following input it works (Relation is created):

mutation {
  updateClub(data: {
    members: {
      connect: {
        id: "cjrwjunmf000o08537cx8k1ib"
      }
    }
  }, where: {
    id: "cjrv8z60q00100854w6ksg6bm"
  }) {
    id
  }
}

However executing my mutation on my node.js server using the GraphQL Playground does not. No error is spit out but no relation is created, as querying clubs on the User or members on the club that I am setting the relationships on have any data, they just return null

Here is the mutation I’m executing that is failing:

mutation {
  addUserToClub(clubid: "cjrv8z60q00100854w6ksg6bm", id: "cjrwjunmf000o08537cx8k1ib") {
    id
    name
    members {
      id
      firstName
    }
  }
}

which responds with:

{
  "data": {
    "addUserToClub": {
      "id": "cjrv8z60q00100854w6ksg6bm",
      "name": "Mikes Awesome Cluuuub",
      "members": null
    }
  }
}

I’ve tried going the opposite direction - e.g. updating the User and setting the “clubs” - but that has not worked. I’ve also verified that changing my mutation to just update a value on the user by just changing say, the name, works.

Anyone see anything obviously wrong with the syntax above or have any ideas why this might not be working? Thanks!


#2

Have you checked the possibility of the relation being created but not returned? Just run the following query right after your mutation:

query {
  clubs {
    members {
     id
    }
  }
}

#3

Hi Benioit,
Yes, I tried the following query (amongst others) as well as returned the Club and User straight away from the mutation:

query { 
	clubs {
    name
    description
    members{
      id
    }
  }
}

which results in:

 {
  "data": {
    "clubs": [
      {
        "name": "Mikes Awesome Cluuuub",
        "description": "This is the best club ever.",
        "members": null
      },
      {
        "name": "Film Critics Club",
        "description": "The Greatest club for movies.",
        "members": null
      }
    ]
  }
}

#4

Could you please share your Node schema? Or better yet a repo? The logic is sound, so it must be something else


#5

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.