После долгого чтения форумов и страниц Inte rnet теперь я могу получить общее количество сущностей, отправленных обратно из MongoDB.
Вот Models.py
from mongoengine import Document
from mongoengine.fields import StringField
class User(Document):
meta = {'collection': 'user'}
first_name = StringField(required=True)
last_name = StringField(required=True)
Вот файл Schema.py
from graphene_mongo import MongoengineObjectType,MongoengineConnectionField
import graphene
from graphene.relay import Node
from Models import User as UserModel
from mongoengine import connect
from flask import Flask
from flask_graphql import GraphQLView
connect(db="graphene-mongo-example",host="127.0.0.1:27017",alias="default")
class Connection(graphene.Connection):
class Meta:
abstract = True
total_count = graphene.Int()
def resolve_total_count(self, info):
return len(self.edges)
class User(MongoengineObjectType):
class Meta:
model = UserModel
interfaces=(Node,)
filter_fields = {'first_name': {'startswith', 'contains'}, 'last_name': [""]}
connection_class = Connection
class Query(graphene.ObjectType):
Node=Node.Field()
all_users = MongoengineConnectionField(User)
schema = graphene.Schema(query=Query)
app = Flask(__name__)
app.debug = True
app.add_url_rule(
"/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True,types=[User])
)
if __name__ == "__main__":
app.run()
Чтобы запустить этот пример:
python Schema.py