A Mongoengine integration for Graphene.
For instaling graphene-mongo, just run this command in your shell
pip install graphene-mongo
Here is a simple Mongoengine model as 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)
To create a GraphQL schema for it you simply have to write the following:
import graphene
from graphene_mongo import MongoengineObjectType
from .models import User as UserModel
class User(MongoengineObjectType):
class Meta:
model = UserModel
class Query(graphene.ObjectType):
users = graphene.List(User)
def resolve_users(self, info):
return list(UserModel.objects.all())
schema = graphene.Schema(query=Query)
Then you can simply query the schema:
query = '''
query {
users {
firstName,
lastName
}
}
'''
result = schema.execute(query)
To learn more check out the following examples:
- Full example: Flask MongoEngine example
- Examples
- Flask example
- Django example
- Filtering & Paging
- Default filtering enabled with all model's attributes by equal comparison (requester: git-albertomarin)
- Take first, or last n items (credit: alexpantyukhin)
- Filter by global id (requester: bwalsh)
- Filter by reference model id (requester: msholty-fd)
- Support more types (Currently supported)
- Self-reference and list-of-self-reference relationship (requester: mehdiym)
After cloning this repo, ensure dependencies are installed by running:
pip install -r requirements.txt
After developing, the full test suite can be evaluated by running:
make test