How to create graphql type for a django model which has many-to-many field

I have django model named profiles. It has some basic fields and many-to-many field followers. This field contains a list of followers and following peoples

class Profile(models.Model):
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE)
    birth_date = models.DateField(
        null=True,
        blank=True)
    profile_picture = models.ImageField(
        upload_to='user_profile_pictures/',
        blank=True,
        null=True)
    cover_picture = models.ImageField(
        upload_to='user_cover_pictures/',
        blank=True,
        null=True)
    profile_description = models.TextField(
        blank=True,
        null=True)
    profile_rating = models.IntegerField(
        default=0)
    followers = models.ManyToManyField(
        'self',
        symmetrical=False,
        related_name='following',
        blank=True)

I used chatGpt to create a type for this model

class ProfileType(DjangoObjectType):
    class Meta:
        model = Profile
        fields = "__all__"

    followers = graphene.List(lambda: ProfileType)
    following = graphene.List(lambda: ProfileType)
    followers_count = graphene.Int()
    following_count = graphene.Int()

    def resolve_followers(self, info):
        return self.followers.all()

    def resolve_following(self, info):
        return self.following.all()

    def resolve_followers_count(self, info):
        return self.followers.count()

    def resolve_following_count(self, info):
        return self.following.count()

This issue is graphene List doesn't have the all() and count() methods. How I should handle this field?

Back to Top