question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using DjangoFilterConnectionField with my own Connection

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
p02diadacommented, Jun 22, 2018

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

2reactions
changelingcommented, May 14, 2019

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

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using DjangoFilterConnectionField with custom Connection in ...
From my understanding, User node needs to be passed UserConnection on it's meta, and UserConnection needs to be passed User on it's meta....
Read more >
Using DjangoFilterConnectionField with custom Connection in ...
Coding example for the question Using DjangoFilterConnectionField with custom Connection in ... class UserConnection(Connection): extra = graphene.
Read more >
django-filter - Graphene-Python
Filtering¶. Graphene-Django integrates with django-filter (2.x for Python 3 or 1.x for Python 2) to provide filtering of results. See the usage ...
Read more >
Graphene-Python
There are several ways you may want to limit access to data when working ... To limit fields in a GraphQL query simply...
Read more >
graphene-django-filter - PyPI
To use, simply replace all DjangoFilterConnectionField fields with AdvancedDjangoFilterConnectionField fields in your queries. Also,if you create custom ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found