|
8 | 8 | import graphene
|
9 | 9 | from graphene.relay import Node
|
10 | 10 |
|
| 11 | +from ..utils import DJANGO_FILTER_INSTALLED |
11 | 12 | from ..compat import MissingType, RangeField
|
12 | 13 | from ..fields import DjangoConnectionField
|
13 | 14 | from ..types import DjangoObjectType
|
@@ -281,3 +282,85 @@ def resolve_all_reporters(self, args, context, info):
|
281 | 282 | }]
|
282 | 283 | }
|
283 | 284 | }
|
| 285 | + |
| 286 | + |
| 287 | +@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED, |
| 288 | + reason="django-filter should be installed") |
| 289 | +def test_should_query_node_filtering(): |
| 290 | + class ReporterType(DjangoObjectType): |
| 291 | + |
| 292 | + class Meta: |
| 293 | + model = Reporter |
| 294 | + interfaces = (Node, ) |
| 295 | + |
| 296 | + class ArticleType(DjangoObjectType): |
| 297 | + |
| 298 | + class Meta: |
| 299 | + model = Article |
| 300 | + interfaces = (Node, ) |
| 301 | + filter_fields = ('lang', ) |
| 302 | + |
| 303 | + class Query(graphene.ObjectType): |
| 304 | + all_reporters = DjangoConnectionField(ReporterType) |
| 305 | + |
| 306 | + r = Reporter.objects.create( |
| 307 | + first_name='John', |
| 308 | + last_name='Doe', |
| 309 | + |
| 310 | + a_choice=1 |
| 311 | + ) |
| 312 | + Article.objects.create( |
| 313 | + headline='Article Node 1', |
| 314 | + pub_date=datetime.date.today(), |
| 315 | + reporter=r, |
| 316 | + editor=r, |
| 317 | + lang='es' |
| 318 | + ) |
| 319 | + Article.objects.create( |
| 320 | + headline='Article Node 2', |
| 321 | + pub_date=datetime.date.today(), |
| 322 | + reporter=r, |
| 323 | + editor=r, |
| 324 | + lang='en' |
| 325 | + ) |
| 326 | + |
| 327 | + schema = graphene.Schema(query=Query) |
| 328 | + query = ''' |
| 329 | + query NodeFilteringQuery { |
| 330 | + allReporters { |
| 331 | + edges { |
| 332 | + node { |
| 333 | + id |
| 334 | + articles(lang: "es") { |
| 335 | + edges { |
| 336 | + node { |
| 337 | + id |
| 338 | + } |
| 339 | + } |
| 340 | + } |
| 341 | + } |
| 342 | + } |
| 343 | + } |
| 344 | + } |
| 345 | + ''' |
| 346 | + |
| 347 | + expected = { |
| 348 | + 'allReporters': { |
| 349 | + 'edges': [{ |
| 350 | + 'node': { |
| 351 | + 'id': 'UmVwb3J0ZXJUeXBlOjE=', |
| 352 | + 'articles': { |
| 353 | + 'edges': [{ |
| 354 | + 'node': { |
| 355 | + 'id': 'QXJ0aWNsZVR5cGU6MQ==' |
| 356 | + } |
| 357 | + }] |
| 358 | + } |
| 359 | + } |
| 360 | + }] |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + result = schema.execute(query) |
| 365 | + assert not result.errors |
| 366 | + assert result.data == expected |
0 commit comments