-1

I am creating schemas in GraphQl and testing these in Playground. These seem to work fine in being able to create Club, User and Team, yet I only want to be able to create a team if a club exists and can therefore connect to the club. At the moment I can create a team without a club existing, which of course shouldn't happen. On create Team Mutation, I also want to stop any club being created, the club must exist prior to any mutation on a Team.

Here is what I have at the moment for my schemas

type Club {
 id: ID! @unique
 name: String! @unique
 team: [Team!]! @relation(name: "TeamToClub", onDelete: CASCADE)
 admin: User! @relation(name: "UserToClub", onDelete: SET_NULL)
 }

type User {
  id: ID! @unique
  name: String!
  team: [Team!]! @relation(name: "TeamToUser", onDelete: SET_NULL)
  club: [Club!]! @relation(name: "UserToClub", onDelete: SET_NULL)
}

type Team {
 id: ID! @unique
 name: String!
 club: Club! @relation(name: "TeamToClub", onDelete: SET_NULL)
 creator: User! @relation(name: "TeamToUser", onDelete: SET_NULL)
 coach:[Coach!]! @relation(name: "CoachToTeam", onDelete: SET_NULL)
}

Here are my mutations for creating a user

mutation {
  createUser(
    data:{
      name:"Jack Jones",
      email:"jack@example.com"
    }
  ){
    id
    name
  }
}

and creating a team..

mutation {
  createTeam(
    data:{
      title:"Barcelona FC"
      agegroup:"Under 12s",
      published: false,
      coachcreator:{
        connect:{
          id:"cka8qkd5h02dj0815m3odzh5s"
        }
      }
    }
  ){
    id
    title
    agegroup
    published
    coachcreator {
      name
    }
  }
}

both of which work without connecting to a club.

Could anybody provide a possible solution to ensure a condition is in place to say a team can only be created when a club already exists.

Mr J
  • 3
  • 5

1 Answers1

0

Strange mutations - looks like they are completely not related to types.

Usually create mutation returns created type, f.e. createUser returns User ... and mutation argument - input type (UserInput) - with almost the same shape as returned type (without id).

The simplest method to ensure that createTeam has proper (existing) club relation is to define club as required argument of mutation.

xadm
  • 8,219
  • 3
  • 14
  • 25