这是我第一次使用石墨烯,对它没有很好的掌握。所以基本上是制作一个博客,用户可以在其中喜欢帖子、评论并将帖子添加到他的收藏夹中,并互相关注。
我为所有用户操作制作了一个单独的模型
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
因此,我对所有操作进行了突变,我正在尝试遵循 DRY 原则并将它们全部汇总,我可能在这里做错了,新编码员尽我所能: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 )
很抱歉问题中的缩进,但在我的代码中完全没问题。
createUaction 突变给了我这个错误
"message": "Field \"createUaction\" of type \"CreateUaction\" must have a sub selection.",
任何帮助表示赞赏。如果我也需要发布解析器,请告诉我。