graphql-python / graphene-mongo

Graphene MongoEngine integration
http://graphene-mongo.readthedocs.io/en/latest/
MIT License
288 stars 113 forks source link

graphene-mongo not compatible with pymongo 4.0 or above #197

Closed leonardwellthy closed 2 years ago

leonardwellthy commented 2 years ago

Error message

Running the tutorial example at https://graphene-mongo.readthedocs.io/en/latest/tutorial.html returns the following error

{
  "errors": [
    {
      "message": "'Cursor' object has no attribute 'count'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "allEmployees"
      ]
    }
  ],
  "data": {
    "allEmployees": null
  }
}

Underlying cause

pymongo.cursor.Cursor.count() was deprecated in pymongo 3.7.0 and removed in pymongo 4.0. See the pymongo changelogs

Solution

The following line in fields.py needs to be replaced with a call to pymongo.collection.Collection.count_documents

count = mongoengine.get_db()[self.model._get_collection_name()].find(args_copy).count()

Temporary workaround

pip install pymongo==3.12.0

krawalli commented 2 years ago

yes, its working when line is changed to:

count = mongoengine.get_db()[self.model._get_collection_name()].count_documents(args_copy)