Wrong code example on Many-To-Many relationship

The following code that can be found in the documentation seems to be wrong:

type Organization @entity {
  id: ID!
  name: String!
  members: [UserOrganization]! @derivedFrom(field: "user")
}

type User @entity {
  id: ID!
  name: String!
  organizations: [UserOrganization!] @derivedFrom(field: "organization")
}

type UserOrganization @entity {
  id: ID!   # Set to `${user.id}-${organization.id}`
  user: User!
  organization: Organization!
}

And should have the @derivedFrom parameter switched like this:

type Organization @entity {
  id: ID!
  name: String!
  members: [UserOrganization]! @derivedFrom(field: "organization")
}

type User @entity {
  id: ID!
  name: String!
  organizations: [UserOrganization!] @derivedFrom(field: "user")
}

type UserOrganization @entity {
  id: ID!   # Set to `${user.id}-${organization.id}`
  user: User!
  organization: Organization!
}
2 Likes

Yeah, I think you’re completely right.

It’s probably an honest mistake, since it’s quite easy to get confused when deriving arrays from entity links :slight_smile:

2 Likes

Have you the mapping.ts of this example? I am trying to do something like this example

Hey @Eibriel! Thanks so much - the docs are now open source, I created a PR with this fix here: Fix reverse lookups example by azf20 · Pull Request #49 · graphprotocol/docs · GitHub