|
5 | 5 | from django.utils.functional import SimpleLazyObject
|
6 | 6 | from py.test import raises
|
7 | 7 |
|
| 8 | +from django.db.models import Q |
| 9 | + |
8 | 10 | import graphene
|
9 | 11 | from graphene.relay import Node
|
10 | 12 |
|
|
17 | 19 | Article,
|
18 | 20 | CNNReporter,
|
19 | 21 | Reporter,
|
| 22 | + Film, |
| 23 | + FilmDetails, |
20 | 24 | )
|
21 | 25 |
|
22 | 26 | pytestmark = pytest.mark.django_db
|
@@ -431,6 +435,60 @@ class Query(graphene.ObjectType):
|
431 | 435 | assert result.data == expected
|
432 | 436 |
|
433 | 437 |
|
| 438 | +@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED, |
| 439 | + reason="django-filter should be installed") |
| 440 | +def test_should_query_node_filtering_with_distinct_queryset(): |
| 441 | + class FilmType(DjangoObjectType): |
| 442 | + |
| 443 | + class Meta: |
| 444 | + model = Film |
| 445 | + interfaces = (Node, ) |
| 446 | + filter_fields = ('genre',) |
| 447 | + |
| 448 | + class Query(graphene.ObjectType): |
| 449 | + films = DjangoConnectionField(FilmType) |
| 450 | + |
| 451 | + # def resolve_all_reporters_with_berlin_films(self, args, context, info): |
| 452 | + # return Reporter.objects.filter(Q(films__film__location__contains="Berlin") | Q(a_choice=1)) |
| 453 | + |
| 454 | + def resolve_films(self, info, **args): |
| 455 | + return Film.objects.filter(Q(details__location__contains="Berlin") | Q(genre__in=['ot'])).distinct() |
| 456 | + |
| 457 | + f = Film.objects.create( |
| 458 | + ) |
| 459 | + fd = FilmDetails.objects.create( |
| 460 | + location="Berlin", |
| 461 | + film=f |
| 462 | + ) |
| 463 | + |
| 464 | + schema = graphene.Schema(query=Query) |
| 465 | + query = ''' |
| 466 | + query NodeFilteringQuery { |
| 467 | + films { |
| 468 | + edges { |
| 469 | + node { |
| 470 | + genre |
| 471 | + } |
| 472 | + } |
| 473 | + } |
| 474 | + } |
| 475 | + ''' |
| 476 | + |
| 477 | + expected = { |
| 478 | + 'films': { |
| 479 | + 'edges': [{ |
| 480 | + 'node': { |
| 481 | + 'genre': 'OT' |
| 482 | + } |
| 483 | + }] |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + result = schema.execute(query) |
| 488 | + assert not result.errors |
| 489 | + assert result.data == expected |
| 490 | + |
| 491 | + |
434 | 492 | @pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
|
435 | 493 | reason="django-filter should be installed")
|
436 | 494 | def test_should_query_node_multiple_filtering():
|
@@ -676,7 +734,7 @@ class Query(graphene.ObjectType):
|
676 | 734 |
|
677 | 735 | def resolve_all_reporters(self, info, **args):
|
678 | 736 | return Promise.resolve([Reporter(id=1)])
|
679 |
| - |
| 737 | + |
680 | 738 | schema = graphene.Schema(query=Query)
|
681 | 739 | query = '''
|
682 | 740 | query ReporterPromiseConnectionQuery {
|
@@ -724,7 +782,7 @@ class Query(graphene.ObjectType):
|
724 | 782 |
|
725 | 783 | def resolve_all_reporters(self, info, **args):
|
726 | 784 | return Reporter.objects.all()
|
727 |
| - |
| 785 | + |
728 | 786 | schema = graphene.Schema(query=Query)
|
729 | 787 | query = '''
|
730 | 788 | query ReporterLastQuery {
|
@@ -779,7 +837,7 @@ class Query(graphene.ObjectType):
|
779 | 837 |
|
780 | 838 | def resolve_all_reporters(self, info, **args):
|
781 | 839 | return Reporter.objects.all()
|
782 |
| - |
| 840 | + |
783 | 841 | schema = graphene.Schema(query=Query)
|
784 | 842 | query = '''
|
785 | 843 | query ReporterLastQuery {
|
@@ -1012,7 +1070,7 @@ def test_proxy_model_fails():
|
1012 | 1070 | """
|
1013 | 1071 | This test asserts that if you try to query for a proxy model,
|
1014 | 1072 | that query will fail with:
|
1015 |
| - GraphQLError('Expected value of type "CNNReporterType" but got: |
| 1073 | + GraphQLError('Expected value of type "CNNReporterType" but got: |
1016 | 1074 | CNNReporter.',)
|
1017 | 1075 |
|
1018 | 1076 | This is because a proxy model has the identical model definition
|
|
0 commit comments