Skip to content

Slicing is ignored when using DjangoFilterConnectionField with a custom resolver #127

Closed
@NiallBrickell

Description

@NiallBrickell

This test currently fails:

def test_should_query_filter_node_limit():
    class ReporterFilter(FilterSet):
        limit = NumberFilter(method='filter_limit')

        def filter_limit(self, queryset, name, value):
            return queryset[:value]

        class Meta:
            model = Reporter
            fields = ['first_name', ]

    class ReporterType(DjangoObjectType):

        class Meta:
            model = Reporter
            interfaces = (Node, )

    class ArticleType(DjangoObjectType):

        class Meta:
            model = Article
            interfaces = (Node, )
            filter_fields = ('lang', )

    class Query(graphene.ObjectType):
        all_reporters = DjangoFilterConnectionField(
            ReporterType,
            filterset_class=ReporterFilter
        )

        def resolve_all_reporters(self, args, context, info):
            return Reporter.objects.all()

    r = Reporter.objects.create(
        first_name='John',
        last_name='Doe',
        email='[email protected]',
        a_choice=1
    )
    Reporter.objects.create(
        first_name='Bob',
        last_name='Doe',
        email='[email protected]',
        a_choice=1
    )

    Article.objects.create(
        headline='Article Node 1',
        pub_date=datetime.date.today(),
        reporter=r,
        editor=r,
        lang='es'
    )
    Article.objects.create(
        headline='Article Node 2',
        pub_date=datetime.date.today(),
        reporter=r,
        editor=r,
        lang='en'
    )

    schema = graphene.Schema(query=Query)
    query = '''
        query NodeFilteringQuery {
            allReporters(limit: 1) {
                edges {
                    node {
                        id
                        articles(lang: "es") {
                            edges {
                                node {
                                    id
                                }
                            }
                        }
                    }
                }
            }
        }
    '''

    expected = {
        'allReporters': {
            'edges': [{
                'node': {
                    'id': 'UmVwb3J0ZXJUeXBlOjE=',
                    'articles': {
                        'edges': [{
                            'node': {
                                'id': 'QXJ0aWNsZVR5cGU6MQ=='
                            }
                        }]
                    }
                }
            }]
        }
    }

    result = schema.execute(query)
    assert not result.errors
    assert result.data == expected

Looks like an issue with https://github.com/graphql-python/graphene-django/blob/master/graphene_django/fields.py#L57. default_manager is the correct, sliced, queryset. However, as it gets intersected with the queryset from the custom resolver (Reporter.objects.all()), the filter's slice is ignored.

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