Skip to content

Support required=True for DjangoFilterConnectionFields #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphene_django/filter/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def get_resolver(self, parent_resolver):
return partial(
self.connection_resolver,
parent_resolver,
self.type,
self.connection_type,
self.get_manager(),
self.max_limit,
self.enforce_first_or_last,
Expand Down
35 changes: 35 additions & 0 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,38 @@ class Query(ObjectType):
}
"""
)


def test_required_filter_field():
class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
filter_fields = ()

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterType, required=True)

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

Reporter.objects.create(first_name="John", last_name="Doe")
schema = Schema(query=Query)

query = """
query NodeFilteringQuery {
allReporters(first: 1) {
edges {
node {
firstName
}
}
}
}
"""
expected = {"allReporters": {"edges": [{"node": {"firstName": "John"}}]}}

result = schema.execute(query)

assert not result.errors
assert result.data == expected
42 changes: 42 additions & 0 deletions graphene_django/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from graphene import ObjectType, Schema
from graphene.relay import Node
from graphene_django import DjangoConnectionField, DjangoObjectType
from graphene_django.tests.models import Article, Pet, Reporter

pytestmark = pytest.mark.django_db


def test_required_connection_field():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between this test and test_required_filter_field ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zbyte64 this test uses a DjangoConnectionField while the other test uses a DjangoFilterConnectionField

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

class Query(ObjectType):
all_reporters = DjangoConnectionField(ReporterType, required=True)

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

Reporter.objects.create(first_name="John", last_name="Doe")

schema = Schema(query=Query)
query = """
query NodeFilteringQuery {
allReporters {
edges {
node {
firstName
}
}
}
}
"""

expected = {"allReporters": {"edges": [{"node": {"firstName": "John"}}]}}

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