Skip to content

Commit 7263485

Browse files
authored
Merge pull request #194 from dvdmgl/173-annotation-bug
Fix graphene 1.3 annotation bug
2 parents 7c52aa3 + b3c761b commit 7263485

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

graphene_django/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_manager(self):
5959

6060
@classmethod
6161
def merge_querysets(cls, default_queryset, queryset):
62-
return default_queryset & queryset
62+
return queryset & default_queryset
6363

6464
@classmethod
6565
def resolve_connection(cls, connection, default_manager, args, iterable):

graphene_django/tests/test_query.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,65 @@ def resolve_all_reporters(self, args, context, info):
284284
}
285285

286286

287+
def test_should_keep_annotations():
288+
from django.db.models import (
289+
Count,
290+
Avg,
291+
)
292+
293+
class ReporterType(DjangoObjectType):
294+
295+
class Meta:
296+
model = Reporter
297+
interfaces = (Node, )
298+
only_fields = ('articles', )
299+
300+
class ArticleType(DjangoObjectType):
301+
302+
class Meta:
303+
model = Article
304+
interfaces = (Node, )
305+
filter_fields = ('lang', )
306+
307+
class Query(graphene.ObjectType):
308+
all_reporters = DjangoConnectionField(ReporterType)
309+
all_articles = DjangoConnectionField(ArticleType)
310+
311+
def resolve_all_reporters(self, args, context, info):
312+
return Reporter.objects.annotate(articles_c=Count('articles')).order_by('articles_c')
313+
314+
def resolve_all_articles(self, args, context, info):
315+
return Article.objects.annotate(import_avg=Avg('importance')).order_by('import_avg')
316+
317+
schema = graphene.Schema(query=Query)
318+
query = '''
319+
query ReporterConnectionQuery {
320+
allReporters {
321+
pageInfo {
322+
hasNextPage
323+
}
324+
edges {
325+
node {
326+
id
327+
}
328+
}
329+
}
330+
allArticles {
331+
pageInfo {
332+
hasNextPage
333+
}
334+
edges {
335+
node {
336+
id
337+
}
338+
}
339+
}
340+
}
341+
'''
342+
result = schema.execute(query)
343+
assert not result.errors
344+
345+
287346
@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
288347
reason="django-filter should be installed")
289348
def test_should_query_node_filtering():

0 commit comments

Comments
 (0)