graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

Using DjangoFilterConnectionField with my own Connection

Open a3ammar opened this issue 8 years ago • 9 comments

Hi guys, I've been trying to use DjangoFilterConnectionField with my own Connection but it only accepts DjangoObjectType. I saw that 15664bdc0b76d9118d41204922a41b808d5848ad attempts to implement it but it got reverted in 4cc46736bf7297d3f927115daedd1c332c7a38ef and after 48bcccdac2deaf3e9ad58eb0758ca9f255e3e38e 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)

a3ammar avatar Jan 02 '18 14:01 a3ammar

Why did you close this issue? I am in the same trouble.

p02diada avatar Jun 22 '18 09:06 p02diada

Same here

exisz avatar Apr 05 '19 09:04 exisz

@a3ammar, @phalt Can this Issue be re-opened? Or at least have a link or actual solution posted here? It may be a simple answer, but it is worth addressing.

changeling avatar May 13 '19 14:05 changeling

@p02diada @gotexis @changeling I closed the issue because there was no response from the maintainer. I solved the issue with something similar to this:

import graphene
from graphene import relay
from graphene_django.types import DjangoObjectType

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

    @classmethod
    def get_node(self, info, id):
        return Ingredient.objects.get(id=id)

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

    class Meta:
        node = IngredientType

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

    
class Query(graphene.ObjectType):
    ingredients = relay.ConnectionField(IngredientTypeConnection, search=graphene.String())

    def resolve_ingredients(self, info, **kwargs):
        search = kwargs.get('search', None)
        if search:
            ingredients = Ingredient.objects.filter(url__icontains=search)
        else:
            ingredients = Ingredient.objects.all()
        return ingredients

You can define your filter fields like what I did with search.

a3ammar avatar May 14 '19 11:05 a3ammar

@a3ammar Thanks for adding this! I solved it with something similar, but using DjangoFilterConnectionField, here:

https://github.com/graphql-python/graphene-django/issues/636

changeling avatar May 14 '19 16:05 changeling

Let's add this to the wiki as a work-around.

phalt avatar May 15 '19 10:05 phalt

Let's add this to the wiki as a work-around.

When I get some time, I'll play around with it to see what needs fixing, but as is, I get:

AssertionError: Found different types with the same name in the schema: IngredientTypeConnection, IngredientTypeConnection.

Perhaps this is implemented with an earlier graphene or graphene-django?

changeling avatar May 15 '19 15:05 changeling

That error will be from the graphene core library, not graphene-django

phalt avatar May 15 '19 15:05 phalt

Anybody have a workaround for this? I want to use a FilterSet on an interface but its not a DjangoObjectType so I can't.

pfcodes avatar Aug 10 '23 10:08 pfcodes