\"CreateUaction\" 类型的字段 \"createUaction\" 必须有子选择。”
Field \"createUaction\" of type \"CreateUaction\" must have a sub selection."
第一次使用石墨烯,不是很了解。
所以基本上是制作一个博客,用户可以在其中点赞 posts、发表评论并将 posts 添加到他的收藏夹,并相互关注。
我为所有用户操作制作了一个单独的模型
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 Principe 并将它们合二为一,我可能在这里做错了什么,新编码员尽力而为 :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.",
感谢任何帮助。如果我也需要 post 解析器,请告诉我。
这不是您的架构问题,而是您提出请求的方式问题。
来自规范:
A selection set is primarily composed of fields. A field describes one discrete piece of information available to request within a selection set.
Some fields describe complex data or relationships to other data. In order to further explore this data, a field may itself contain a selection set, allowing for deeply nested requests. All GraphQL operations must specify their selections down to fields which return scalar values to ensure an unambiguously shaped response.
换句话说,每个字段都必须解析为具体值(如标量或枚举)。如果字段解析为 ObjectType,则您必须至少请求一个该类型的字段。您 select 的字段称为该字段的子select离子。
该错误表明您缺少 CreateUaction
类型的子select离子。您的请求应该更像这样:
mutation SomeOperationName {
createUaction {
user {
# one or more user fields
}
}
}
第一次使用石墨烯,不是很了解。 所以基本上是制作一个博客,用户可以在其中点赞 posts、发表评论并将 posts 添加到他的收藏夹,并相互关注。
我为所有用户操作制作了一个单独的模型
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 Principe 并将它们合二为一,我可能在这里做错了什么,新编码员尽力而为 :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.",
感谢任何帮助。如果我也需要 post 解析器,请告诉我。
这不是您的架构问题,而是您提出请求的方式问题。
来自规范:
A selection set is primarily composed of fields. A field describes one discrete piece of information available to request within a selection set.
Some fields describe complex data or relationships to other data. In order to further explore this data, a field may itself contain a selection set, allowing for deeply nested requests. All GraphQL operations must specify their selections down to fields which return scalar values to ensure an unambiguously shaped response.
换句话说,每个字段都必须解析为具体值(如标量或枚举)。如果字段解析为 ObjectType,则您必须至少请求一个该类型的字段。您 select 的字段称为该字段的子select离子。
该错误表明您缺少 CreateUaction
类型的子select离子。您的请求应该更像这样:
mutation SomeOperationName {
createUaction {
user {
# one or more user fields
}
}
}