Graphene-django: Using DjangoFilterConnectionField with my own Connection

Created on 2 Jan 2018  路  8Comments  路  Source: graphql-python/graphene-django

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)

Most helpful comment

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

All 8 comments

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

Same here

@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.

@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 Thanks for adding this! I solved it with something similar, but using DjangoFilterConnectionField, here:

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

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

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?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

x9sheikh picture x9sheikh  路  4Comments

hyusetiawan picture hyusetiawan  路  4Comments

mraak picture mraak  路  3Comments

eyalch picture eyalch  路  3Comments

Dawidpol picture Dawidpol  路  4Comments