This is the first time I am using graphene, ain't have a good grasp over it. So basically making a blog, where the user can like posts, comments and add posts to his favourite, and follow each other.
I have made a separate model for all user actions
class user_actions(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
liked_post = models.ForeignKey(Post, related_name='post_likes',
on_delete=models.CASCADE)
liked_comments = models.ForeignKey(Comment,
related_name='comment_likes', on_delete=models.CASCADE)
fav = models.ForeignKey(Post, related_name='fav_post',
on_delete=models.CASCADE)
target = models.ForeignKey(User, related_name='followers',
on_delete=models.CASCADE, null=True, blank = True)
follower = models.ForeignKey(User, related_name='targets',
on_delete=models.CASCADE, null = True, blank = True)
def __str__(self):
return self.user.username
So I have made a mutation for all the actions, I am trying to follow the DRY Principe and sum them all in one, I might be doing something wrong here, New coder trying my best :D
class UactionInput(InputObjectType):
liked_post_id = graphene.Int()
fav_post_id = graphene.Int()
comment_id = graphene.Int()
target_id = graphene.Int()
follower_id = graphene.Int()
class CreateUaction(graphene.Mutation):
user = graphene.Field(UactionType)
class Arguments:
input = UactionInput()
def mutate(self, info, input):
user = info.context.user
if not user.is_authenticated:
return CreateUaction(errors=json.dumps('Please Login '))
if input.liked_post_id:
post = Post.objects.get(id=input.liked_post_id)
user_action = user_actions.objects.create(
liked_post = post,
user = user
)
return CreateUaction( user = user )
if input.liked_comment_id:
comment = Comment.objects.get(id=input.liked_comment_id)
user_action = user_actions.objects.create(
liked_comment = comment,
user = user
)
return CreateUaction(user = user )
if input.fav_post_id:
post = Post.objects.get(id=input.fav_post_id)
user_action = user_actions.objects.create(
fav = post,
user = user
)
return CreateUaction(user = user )
if input.target_id:
user = User.objects.get(id=input.target_id)
user_action = user_actions.objects.create(
target = user,
user = user
)
return CreateUaction(user = user )
if input.follower_id:
user = User.objects.get(id=input.follower_id)
user_action = user_actions.objects.create(
follower= user,
user = user
)
return CreateUaction(user = user )
Sorry for the indentation in the question, but it's completely fine in my code.
The createUaction mutation gives me this error
"message": "Field \"createUaction\" of type \"CreateUaction\" must have a sub selection.",
Any help is appreciated. Do let me know if I need to post the resolvers too.