I'm new to Prisma/GraphQL and want to know how I c...
# orm-help
o
I'm new to Prisma/GraphQL and want to know how I create an association through another table? In my case, Users belong to many Accounts through Memberships, each of these memberships has a role for the user (eg. they could be an admin for one account and a normal user for another). Could someone please help me out?
t
I think something like that:
Copy code
type User {
  id: ID! @unique
  name: String!
  books: [Membership!]! @relation(name: "UserMemberships" onDelete: CASCADE)
}

type Membership {
  id: ID! @unique
  name: String!
  role: Role! @relation(name: "MembershipRole" onDelete: CASCADE)
}

type Role {
  id: ID! @unique
  membership: Membership! @relation(name: "MembershipRole" onDelete: CASCADE)
}
n
That has some issues...
o
I'm not sure I understand how to link Membership with the Account table in order to retrieve its details when I get the user's memberships. Essentially what I want to do is get the Accounts for the User (and vice versa). Here is something I have pieced together based on the code above but that linking still concerns me. @nuno I'm getting
The field 'name' is a scalar field and cannot specify the '@relation' directive.
in Role.
Copy code
type User {
  id: ID! @id
  name: String!
  accounts: [Membership!]! @relation(name: "UserMemberships" onDelete: CASCADE)
}

type Membership {
  id: ID! @id
  account_email: String
  account_phone: String
  role: Role! @relation(name: "MembershipRole" onDelete: CASCADE)
}

type Role {
  id: ID! @id
  name: String! @relation(name: "MembershipRole" onDelete: CASCADE)
  description: String
}

type Account {
  id: ID! @id
  name: String!
  members: [Membership!]!
}
n
Copy code
type User {
  id: ID! @id
  name: String!
  memberships: [Membership!]! @relation(onDelete: CASCADE)
}

type Account {
  id: ID! @id
  name: String!
  memberships: [Membership!]! @relation(onDelete: CASCADE)
}

type Membership {
  id: ID! @id
  role: Role!
  user: User!
  account: Account!
}

enum Role {
  ADMIN
  USER
}
Does this work for you?
o
Thank you. I think it's it! I will give it a good test in the morning.
t
Sorry some spelling mistake in Role type 😃
o
@Taras Protchenko @nuno I am adding resolvers for `User`:
Copy code
const User = {
  memberships: ({ id }, args, context) => {
    return context.prisma.user({ id }).memberships();
  }
};
module.exports = { User };
but it doesn’t return
Account
in
Membership
. I also defined a resolver `Membership`:
Copy code
const Membership = {
  account: ({ account }, args, context) => {
    return context.prisma.membership({ account }).account();
  }
};
module.exports = { Membership };
and get error:
Copy code
"message": "You provided an invalid argument for the where selector on Membership."
n
You're passing an invalid argument in
context.prisma.membership()
. Try:
Copy code
const Membership = {
  account: ({ id }, args, context) => {
    return context.prisma.membership({ id }).account();
  }
};
👍 1