Skip to content

Using DjangoFilterConnectionField with my own Connection #363

Closed
@a3ammar

Description

@a3ammar

Hi guys,
I've been trying to use DjangoFilterConnectionField with my own Connection but it only accepts DjangoObjectType. I saw that 15664bd attempts to implement it but it got reverted in 4cc4673 and after 48bcccd I get an assertion error.

Is this no longer supported? Is there an alternate way?

Basically I'd like to add custom fields like count and have filtering support. In the code below, I can't define count under categories unless I change to ConnectionField:

import graphene
from graphene import Node, Connection, ConnectionField
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from cookbook.ingredients.models import Category, Ingredient


class IngredientType(DjangoObjectType):
    class Meta:
        model = Ingredient
        filter_fields = ['name', 'notes', 'category']
        interfaces = (Node,)


class IngredientConnection(Connection):
    count = graphene.Int()

    class Meta:
        node = IngredientType

    def resolve_count(self, context, **kwargs):
        return self.iterable.count()

    
class CategoryType(DjangoObjectType):
    ingredients = ConnectionField(IngredientConnection)

    class Meta:
        model = Category
        filter_fields = ['name']
        interfaces = (Node,)

    @staticmethod
    def resolve_ingredients(self, context, **kwargs):
        return self.ingredients.all()

    @staticmethod
    def resolve_all(self, context, **kwargs):
        return Category.objects.all()

    
class CategoryConnection(Connection):
    count = graphene.Int()

    class Meta:
        node = CategoryType

    def resolve_count(self, context, **kwargs):
        return self.iterable.count()

    
class Query(graphene.ObjectType):
    categories = DjangoFilterConnectionField(CategoryType)
    # categories = ConnectionField(CategoryConnection, resolver=CategoryType.resolve_all)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions