I`m not able to understand how to make statement so if I make draft 1 and I have two heroes with same ID (or name, for example 'Spectre') it will restrict mutation with an error: 'You can have same hero in the draft'
Bolded text inside user_draft.py is unused because I dont know were or how to make condition. Inside mutate before I save it or?
user_draft.py
import graphene
from graphene_django import DjangoObjectType
from user_draft.models import UserDrafts
from users.schema import UserType
from heroes.schema import HeroType
from users.models import User
from heroes.models import Hero
class UserDraftType(DjangoObjectType):
class Meta:
model = UserDrafts
class Query(graphene.AbstractType):
all_drafts = graphene.Field(lambda: graphene.List(UserDraftType))
def resolve_all_drafts(self, info):
return UserDrafts.objects.all()
class CreateDraft(graphene.Mutation):
message = graphene.String()
user = graphene.Field(UserType)
hero = graphene.Field(HeroType)
class Arguments:
draft = graphene.Int()
hero = graphene.ID()
user = graphene.ID()
@staticmethod
def mutate(self, info, draft, user, hero):
hero_id = Hero.objects.get(id=hero)
user_id = User.objects.get(id=user)
**error_message = 'You cant duplicate heroes'
draft_error = 'You can only pick 1, 2 or 3.'**
draft_data = UserDrafts.objects.create(
draft=draft,
hero=hero_id,
user=user_id,
)
draft_data.save()
return CreateDraft(
message='You created ur hero pool successfully'
)
class Mutation(graphene.ObjectType):
create_draft = CreateDraft.Field()
So inside my CreateDraft mutation I want to be able to restrict user not to have same pick in draft 1 for example or draft 2, also because draft is type intiger I want to be able user to pick only 1, 2 and 3 not to have negative intiger or positive more then 3.
This is first time i need condition inside graphene mutation so I dont know how to do it. Im using posgresql. With this code I`m able to create mutation but i can duplicate heroes and also drafts.
just in case, models.py
from django.db import models
from users.models import User
from heroes.models import Hero
class UserDrafts(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_id")
draft = models.IntegerField()
hero = models.ForeignKey(Hero, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.user.username
class Meta:
verbose_name = "UserDraft"
verbose_name_plural = "UserDrafts"
Aucun commentaire:
Enregistrer un commentaire