|
13 | 13 | from ..fields import DjangoConnectionField |
14 | 14 | from ..types import DjangoObjectType |
15 | 15 | from ..settings import graphene_settings |
16 | | -from .models import Article, Reporter |
| 16 | +from .models import ( |
| 17 | + Article, |
| 18 | + CNNReporter, |
| 19 | + Reporter, |
| 20 | +) |
17 | 21 |
|
18 | 22 | pytestmark = pytest.mark.django_db |
19 | 23 |
|
@@ -689,6 +693,7 @@ class Query(graphene.ObjectType): |
689 | 693 | email='johndoe@example.com', |
690 | 694 | a_choice=1 |
691 | 695 | ) |
| 696 | + |
692 | 697 | Article.objects.create( |
693 | 698 | headline='Article Node 1', |
694 | 699 | pub_date=datetime.date.today(), |
@@ -780,3 +785,139 @@ class Query(graphene.ObjectType): |
780 | 785 | ''' |
781 | 786 | result = schema.execute(query) |
782 | 787 | assert not result.errors |
| 788 | + |
| 789 | + |
| 790 | +def test_proxy_model_support(): |
| 791 | + """ |
| 792 | + This test asserts that we can query for all Reporters, |
| 793 | + even if some are of a proxy model type at runtime. |
| 794 | + """ |
| 795 | + class ReporterType(DjangoObjectType): |
| 796 | + |
| 797 | + class Meta: |
| 798 | + model = Reporter |
| 799 | + interfaces = (Node, ) |
| 800 | + use_connection = True |
| 801 | + |
| 802 | + reporter_1 = Reporter.objects.create( |
| 803 | + first_name='John', |
| 804 | + last_name='Doe', |
| 805 | + email='johndoe@example.com', |
| 806 | + a_choice=1 |
| 807 | + ) |
| 808 | + |
| 809 | + reporter_2 = CNNReporter.objects.create( |
| 810 | + first_name='Some', |
| 811 | + last_name='Guy', |
| 812 | + email='someguy@cnn.com', |
| 813 | + a_choice=1, |
| 814 | + reporter_type=2, # set this guy to be CNN |
| 815 | + ) |
| 816 | + |
| 817 | + class Query(graphene.ObjectType): |
| 818 | + all_reporters = DjangoConnectionField(ReporterType) |
| 819 | + |
| 820 | + schema = graphene.Schema(query=Query) |
| 821 | + query = ''' |
| 822 | + query ProxyModelQuery { |
| 823 | + allReporters { |
| 824 | + edges { |
| 825 | + node { |
| 826 | + id |
| 827 | + } |
| 828 | + } |
| 829 | + } |
| 830 | + } |
| 831 | + ''' |
| 832 | + |
| 833 | + expected = { |
| 834 | + 'allReporters': { |
| 835 | + 'edges': [{ |
| 836 | + 'node': { |
| 837 | + 'id': 'UmVwb3J0ZXJUeXBlOjE=', |
| 838 | + }, |
| 839 | + }, |
| 840 | + { |
| 841 | + 'node': { |
| 842 | + 'id': 'UmVwb3J0ZXJUeXBlOjI=', |
| 843 | + }, |
| 844 | + } |
| 845 | + ] |
| 846 | + } |
| 847 | + } |
| 848 | + |
| 849 | + result = schema.execute(query) |
| 850 | + assert not result.errors |
| 851 | + assert result.data == expected |
| 852 | + |
| 853 | + |
| 854 | +def test_proxy_model_fails(): |
| 855 | + """ |
| 856 | + This test asserts that if you try to query for a proxy model, |
| 857 | + that query will fail with: |
| 858 | + GraphQLError('Expected value of type "CNNReporterType" but got: |
| 859 | + CNNReporter.',) |
| 860 | +
|
| 861 | + This is because a proxy model has the identical model definition |
| 862 | + to its superclass, and defines its behavior at runtime, rather than |
| 863 | + at the database level. Currently, filtering objects of the proxy models' |
| 864 | + type isn't supported. It would require a field on the model that would |
| 865 | + represent the type, and it doesn't seem like there is a clear way to |
| 866 | + enforce this pattern across all projects |
| 867 | + """ |
| 868 | + class CNNReporterType(DjangoObjectType): |
| 869 | + |
| 870 | + class Meta: |
| 871 | + model = CNNReporter |
| 872 | + interfaces = (Node, ) |
| 873 | + use_connection = True |
| 874 | + |
| 875 | + reporter_1 = Reporter.objects.create( |
| 876 | + first_name='John', |
| 877 | + last_name='Doe', |
| 878 | + email='johndoe@example.com', |
| 879 | + a_choice=1 |
| 880 | + ) |
| 881 | + |
| 882 | + reporter_2 = CNNReporter.objects.create( |
| 883 | + first_name='Some', |
| 884 | + last_name='Guy', |
| 885 | + email='someguy@cnn.com', |
| 886 | + a_choice=1, |
| 887 | + reporter_type=2, # set this guy to be CNN |
| 888 | + ) |
| 889 | + |
| 890 | + class Query(graphene.ObjectType): |
| 891 | + all_reporters = DjangoConnectionField(CNNReporterType) |
| 892 | + |
| 893 | + schema = graphene.Schema(query=Query) |
| 894 | + query = ''' |
| 895 | + query ProxyModelQuery { |
| 896 | + allReporters { |
| 897 | + edges { |
| 898 | + node { |
| 899 | + id |
| 900 | + } |
| 901 | + } |
| 902 | + } |
| 903 | + } |
| 904 | + ''' |
| 905 | + |
| 906 | + expected = { |
| 907 | + 'allReporters': { |
| 908 | + 'edges': [{ |
| 909 | + 'node': { |
| 910 | + 'id': 'UmVwb3J0ZXJUeXBlOjE=', |
| 911 | + }, |
| 912 | + }, |
| 913 | + { |
| 914 | + 'node': { |
| 915 | + 'id': 'UmVwb3J0ZXJUeXBlOjI=', |
| 916 | + }, |
| 917 | + } |
| 918 | + ] |
| 919 | + } |
| 920 | + } |
| 921 | + |
| 922 | + result = schema.execute(query) |
| 923 | + assert result.errors |
0 commit comments