Repository: graphql-python/graphene-django

Issue:
graphene 1.3 annotation bug
There seems to be a bug with graphene == 1.3

I have a model that I annotate a field "total_rent"
I think graphene is stripping it away somehow

I get this exception ```Cannot resolve keyword 'total_rent' into field. Choices are ... ```

When reverting back to graphene == 1.2.1 the error goes away.
I wish I could provide a more in depth report but my deadlines are to much at the moment


Discussion:
A code snippet would be useful.
Came across with this issue, following snippet results in `"Cannot resolve keyword 'num_ingredients' into field. Choices are: id, ingredients, name"` message.

```python
class IngredientType(DjangoObjectType):
    class Meta:
        model = Ingredient

class CategoryType(DjangoObjectType):
    class Meta:
        model = Category
        interfaces = (
            graphene.Node,
        )

class Query(graphene.AbstractType):
    all_categories = DjangoConnectionField(CategoryType)
    ingredient = graphene.Field(IngredientType,
                                id=graphene.Int(),
                                name=graphene.String())

    def resolve_all_categories(self, args, context, info):
        return Category.objects.annotate(
            num_ingredients=Count('ingredients')
        ).order_by('num_ingredients')

```
But If removed `order_by` returns the results, hope it helps @adamdonahue 
@dvdmgl  Does the `Category.objects.annotate` call work on its own from the shell? Can you provide a stack trace?
hi @adamdonahue the query works from the shell
```
>>> from cookbook.ingredients.models import Category
>>> from django.db.models import Count
>>> 
>>> Category.objects.annotate(num_ingredients=Count('ingredients')).order_by('num_ingredients')
[<Category: Dairy>, <Category: Meat>]
```
```
{
  allCategories(first:10) {
    edges {
      node {
        name
      }
    }
  }
}
```
```
Traceback (most recent call last):
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/graphql/execution/executor.py", line 200, in resolve_or_error
    return executor.execute(resolve_fn, source, args, context, info)
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/graphql/execution/executors/sync.py", line 7, in execute
    return fn(*args, **kwargs)
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/graphene_django/debug/middleware.py", line 55, in resolve
    promise = next(root, args, context, info)
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/graphql/execution/middleware.py", line 57, in make_it_promise
    return Promise.resolve(next(*a, **b))
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/graphene_django/fields.py", line 105, in connection_resolver
    pageinfo_type=PageInfo,
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/graphql_relay/connection/arrayconnection.py", line 90, in connection_from_list_slice
    for i, node in enumerate(_slice)
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/query.py", line 258, in __iter__
    self._fetch_all()
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 841, in execute_sql
    sql, params = self.as_sql()
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 384, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 49, in pre_sql_setup
    order_by = self.get_order_by()
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 295, in get_order_by
    field, self.query.get_meta(), default_order=asc))
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 570, in find_ordering_name
    field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 603, in _setup_joins
    pieces, opts, alias)
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1405, in setup_joins
    names, opts, allow_many, fail_on_missing=True)
  File "/home/dvdmgl/src/github.com/graphql-python/graphene-django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1330, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'num_ingredients' into field. Choices are: id, ingredients, name
```

```
pip freeze
Django==1.9
graphene==1.4
graphene-django==1.3
graphql-core==1.1
graphql-relay==0.4.5
iso8601==0.1.11
pkg-resources==0.0.0
promise==2.0.2
singledispatch==3.4.0.3
six==1.10.0
typing==3.6.1
```

Hope it helps
@adamdonahue digging a bit more, found the following
https://github.com/graphql-python/graphene-django/pull/151#issuecomment-296294817
https://github.com/graphql-python/graphene-django/issues/179


Resolving patch:
diff --git a/graphene_django/fields.py b/graphene_django/fields.py
--- a/graphene_django/fields.py
+++ b/graphene_django/fields.py
@@ -57,7 +57,7 @@ def get_manager(self):
 
     @classmethod
     def merge_querysets(cls, default_queryset, queryset):
-        return default_queryset & queryset
+        return queryset & default_queryset
 
     @classmethod
     def connection_resolver(cls, resolver, connection, default_manager, max_limit,


Test patch:
diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py
--- a/graphene_django/tests/test_query.py
+++ b/graphene_django/tests/test_query.py
@@ -284,6 +284,65 @@ def resolve_all_reporters(self, args, context, info):
     }
 
 
+def test_should_keep_annotations():
+    from django.db.models import (
+        Count,
+        Avg,
+    )
+
+    class ReporterType(DjangoObjectType):
+
+        class Meta:
+            model = Reporter
+            interfaces = (Node, )
+            only_fields = ('articles', )
+
+    class ArticleType(DjangoObjectType):
+
+        class Meta:
+            model = Article
+            interfaces = (Node, )
+            filter_fields = ('lang', )
+
+    class Query(graphene.ObjectType):
+        all_reporters = DjangoConnectionField(ReporterType)
+        all_articles = DjangoConnectionField(ArticleType)
+
+        def resolve_all_reporters(self, args, context, info):
+            return Reporter.objects.annotate(articles_c=Count('articles')).order_by('articles_c')
+
+        def resolve_all_articles(self, args, context, info):
+            return Article.objects.annotate(import_avg=Avg('importance')).order_by('import_avg')
+
+    schema = graphene.Schema(query=Query)
+    query = '''
+        query ReporterConnectionQuery {
+          allReporters {
+            pageInfo {
+              hasNextPage
+            }
+            edges {
+              node {
+                id
+              }
+            }
+          }
+          allArticles {
+            pageInfo {
+              hasNextPage
+            }
+            edges {
+              node {
+                id
+              }
+            }
+          }
+        }
+    '''
+    result = schema.execute(query)
+    assert not result.errors
+
+
 @pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
                     reason="django-filter should be installed")
 def test_should_query_node_filtering():