Reputation: 161
Let's say I've got a models.py with two tables:
class Category(models.Model):
cat = models.CharField(max_length=100)
class Thing(models.Model):
desc = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
and my schemas as follows:
class ThingType(DjangoObjectType):
class Meta:
model = Thing
class FindThing(graphene.ObjectType):
things = graphene.List(
ThingType,
search=graphene.String(),
thing=graphene.ID(),
)
def resolve_things(self, info, thing=None, search=None, **kwargs):
qs = Thing.objects.all()
if search:
filter = (
Q(desc__icontains=search)
)
qs = qs.filter(filter)
if thing:
qs = qs.filter(id=thing)
return qs
class CreateThing(graphene.Mutation):
id = graphene.Int()
desc = graphene.String()
category = graphene.Field(FindCategory)
class Arguments:
desc = graphene.String()
category = graphene.Int()
def mutate(self, info, desc, category):
thing = Thing(
desc=desc,
category=Category.objects.get(id=category)
)
thing.save()
return CreateThing(
id=thing.id,
desc=thing.desc,
category=thing.category_id
)
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class GetCategory(graphene.ObjectType):
category = graphene.List(
CategoryType,
category=graphene.String(),
)
def resolve_category(self, info, category=None, **kwargs):
qs = Category.objects.all()
if category:
get = (
Q(category__contains=category)
)
qs = qs.get(get)
return qs
class FindCategory(graphene.ObjectType):
categories = graphene.List(
CategoryType,
search=graphene.String(),
cat=graphene.ID(),
)
def resolve_categories(self, info, cat=None, search=None, **kwargs):
qs = Category.objects.all()
if search:
filter = (
Q(cat__icontains=search)
)
qs = qs.filter(filter)
if cat:
qs = qs.filter(id=cat)
return qs
class CreateCategory(graphene.Mutation):
id = graphene.Int()
cat = graphene.String()
desc = graphene.String()
class Arguments:
cat = graphene.String()
desc = graphene.String()
def mutate(self, info, cat, desc):
category = Category(
cat=cat
)
category.save()
thing = Thing(
desc=desc,
category_id=category.id
)
thing.save()
return CreateCategory(
id=category.id,
cat=category.cat,
desc=thing.desc,
)
I've managed to create a schema where one can create a new category that already links to a newly created single thing:
mutation createCategory{
createCategory(cat:"cat7", desc:"defg"){
id
cat
desc
}
}
Is it possible to create a CreateCategory django-graphene schema where one can create a category with multiple additional new things?
Upvotes: 2
Views: 767
Reputation: 330
You could allow the CreateCategory mutation to accept a list of descriptions for multiple things:
CreateCategory:
descs = graphene.List(String)
class Arguments:
descs = graphene.List(String)
and then loop over this list inside the mutate function:
new_things = []
for desc in descs:
thing = Thing(
desc=desc,
category_id=category.id
)
thing.save()
new_things.append(thing.desc)
return CreateCategory(
id=category.id,
cat=category.cat,
descs=new_things
)
Upvotes: 1