Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field \"createUaction\" of type \"CreateUaction\" must have a sub selection."

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.

like image 812
Abhijeet Pal Avatar asked Dec 24 '18 07:12

Abhijeet Pal


1 Answers

This is not an issue with your schema, but with how you're making the request.

From the specification:

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.

In other words, every field must resolve to a concrete value (like a scalar or enum). If a field resolves to an ObjectType, you have to request at least one of the fields of that type. The fields you select are called the subselection for that field.

The error indicates you are missing a subselection for the type CreateUaction. Your request should look more like this:

mutation SomeOperationName {
  createUaction {
    user {
      # one or more user fields
    }
  }
}
like image 154
Daniel Rearden Avatar answered Sep 19 '22 13:09

Daniel Rearden