|
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 |
|
@@ -844,6 +848,7 @@ class Query(graphene.ObjectType):
|
844 | 848 |
|
845 | 849 | a_choice=1
|
846 | 850 | )
|
| 851 | + |
847 | 852 | Article.objects.create(
|
848 | 853 | headline='Article Node 1',
|
849 | 854 | pub_date=datetime.date.today(),
|
@@ -937,3 +942,139 @@ class Query(graphene.ObjectType):
|
937 | 942 | '''
|
938 | 943 | result = schema.execute(query)
|
939 | 944 | assert not result.errors
|
| 945 | + |
| 946 | + |
| 947 | +def test_proxy_model_support(): |
| 948 | + """ |
| 949 | + This test asserts that we can query for all Reporters, |
| 950 | + even if some are of a proxy model type at runtime. |
| 951 | + """ |
| 952 | + class ReporterType(DjangoObjectType): |
| 953 | + |
| 954 | + class Meta: |
| 955 | + model = Reporter |
| 956 | + interfaces = (Node, ) |
| 957 | + use_connection = True |
| 958 | + |
| 959 | + reporter_1 = Reporter.objects.create( |
| 960 | + first_name='John', |
| 961 | + last_name='Doe', |
| 962 | + |
| 963 | + a_choice=1 |
| 964 | + ) |
| 965 | + |
| 966 | + reporter_2 = CNNReporter.objects.create( |
| 967 | + first_name='Some', |
| 968 | + last_name='Guy', |
| 969 | + |
| 970 | + a_choice=1, |
| 971 | + reporter_type=2, # set this guy to be CNN |
| 972 | + ) |
| 973 | + |
| 974 | + class Query(graphene.ObjectType): |
| 975 | + all_reporters = DjangoConnectionField(ReporterType) |
| 976 | + |
| 977 | + schema = graphene.Schema(query=Query) |
| 978 | + query = ''' |
| 979 | + query ProxyModelQuery { |
| 980 | + allReporters { |
| 981 | + edges { |
| 982 | + node { |
| 983 | + id |
| 984 | + } |
| 985 | + } |
| 986 | + } |
| 987 | + } |
| 988 | + ''' |
| 989 | + |
| 990 | + expected = { |
| 991 | + 'allReporters': { |
| 992 | + 'edges': [{ |
| 993 | + 'node': { |
| 994 | + 'id': 'UmVwb3J0ZXJUeXBlOjE=', |
| 995 | + }, |
| 996 | + }, |
| 997 | + { |
| 998 | + 'node': { |
| 999 | + 'id': 'UmVwb3J0ZXJUeXBlOjI=', |
| 1000 | + }, |
| 1001 | + } |
| 1002 | + ] |
| 1003 | + } |
| 1004 | + } |
| 1005 | + |
| 1006 | + result = schema.execute(query) |
| 1007 | + assert not result.errors |
| 1008 | + assert result.data == expected |
| 1009 | + |
| 1010 | + |
| 1011 | +def test_proxy_model_fails(): |
| 1012 | + """ |
| 1013 | + This test asserts that if you try to query for a proxy model, |
| 1014 | + that query will fail with: |
| 1015 | + GraphQLError('Expected value of type "CNNReporterType" but got: |
| 1016 | + CNNReporter.',) |
| 1017 | +
|
| 1018 | + This is because a proxy model has the identical model definition |
| 1019 | + to its superclass, and defines its behavior at runtime, rather than |
| 1020 | + at the database level. Currently, filtering objects of the proxy models' |
| 1021 | + type isn't supported. It would require a field on the model that would |
| 1022 | + represent the type, and it doesn't seem like there is a clear way to |
| 1023 | + enforce this pattern across all projects |
| 1024 | + """ |
| 1025 | + class CNNReporterType(DjangoObjectType): |
| 1026 | + |
| 1027 | + class Meta: |
| 1028 | + model = CNNReporter |
| 1029 | + interfaces = (Node, ) |
| 1030 | + use_connection = True |
| 1031 | + |
| 1032 | + reporter_1 = Reporter.objects.create( |
| 1033 | + first_name='John', |
| 1034 | + last_name='Doe', |
| 1035 | + |
| 1036 | + a_choice=1 |
| 1037 | + ) |
| 1038 | + |
| 1039 | + reporter_2 = CNNReporter.objects.create( |
| 1040 | + first_name='Some', |
| 1041 | + last_name='Guy', |
| 1042 | + |
| 1043 | + a_choice=1, |
| 1044 | + reporter_type=2, # set this guy to be CNN |
| 1045 | + ) |
| 1046 | + |
| 1047 | + class Query(graphene.ObjectType): |
| 1048 | + all_reporters = DjangoConnectionField(CNNReporterType) |
| 1049 | + |
| 1050 | + schema = graphene.Schema(query=Query) |
| 1051 | + query = ''' |
| 1052 | + query ProxyModelQuery { |
| 1053 | + allReporters { |
| 1054 | + edges { |
| 1055 | + node { |
| 1056 | + id |
| 1057 | + } |
| 1058 | + } |
| 1059 | + } |
| 1060 | + } |
| 1061 | + ''' |
| 1062 | + |
| 1063 | + expected = { |
| 1064 | + 'allReporters': { |
| 1065 | + 'edges': [{ |
| 1066 | + 'node': { |
| 1067 | + 'id': 'UmVwb3J0ZXJUeXBlOjE=', |
| 1068 | + }, |
| 1069 | + }, |
| 1070 | + { |
| 1071 | + 'node': { |
| 1072 | + 'id': 'UmVwb3J0ZXJUeXBlOjI=', |
| 1073 | + }, |
| 1074 | + } |
| 1075 | + ] |
| 1076 | + } |
| 1077 | + } |
| 1078 | + |
| 1079 | + result = schema.execute(query) |
| 1080 | + assert result.errors |
0 commit comments