graphqlgraphql-jsapollo-servergraphcool

graphql: adding a field to a join table


I have two sets of records with the following schema

type Player {
    id: ID! @unique
    name: String!
    links: [NPC!]! @relation(name: 'linkedNpcs')
}

type NPC {
        id: ID! @unique
        name: String!
        playerLinks: [Player!]! @relation(name: 'linkedNpcs')
}

I want to add an additional field to the linkedNpcs called relationType.

How would I write this in my types.graphql file? Each player-npc link should contain a unique record, and since it's a many-to-many relationship, it makes sense that this metaData should exist on the join table.


Solution

  • answered via graphcool forums: https://www.graph.cool/forum/t/adding-a-field-to-a-relation-table-in-prisma/3086/2

    thanks @matic!

    enum RelationType {
      A
      B
      C
    }
    
    type Player {
      id: ID! @unique
      name: String!
      links: [PlayerNPC!]! @relation(name: 'linkedPlayer')
    }
    
    type PlayerNPC {
      id: ID! @unique
    
      relationType: RelationType!
    
      player: Player! @relation(name: 'linkedPlayer')
      npc: NPC! @relation(name: 'linkedNPC')
    }
    
    type NPC {
      id: ID! @unique
      name: String!
      links: [PlayerNPC!]! @relation(name: ‘linkedNPC’)
    }