Graphene Mongoengine integration
Project description
Graphene-Mongo
A Mongoengine integration for Graphene.
Installation
For installing graphene-mongo, just run this command in your shell
pip install graphene-mongo
Examples
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 and sync executor; 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 = await schema.execute(query)
To create a GraphQL schema and async executor; for it you simply have to write the following:
import graphene
from graphene_mongo import AsyncMongoengineObjectType
from asgiref.sync import sync_to_async
from concurrent.futures import ThreadPoolExecutor
from .models import User as UserModel
class User(AsyncMongoengineObjectType):
class Meta:
model = UserModel
class Query(graphene.ObjectType):
users = graphene.List(User)
async def resolve_users(self, info):
return await sync_to_async(list, thread_sensitive=False,
executor=ThreadPoolExecutor())(UserModel.objects.all())
schema = graphene.Schema(query=Query)
Then you can simply query the schema:
query = '''
query {
users {
firstName,
lastName
}
}
'''
result = await schema.execute_async(query)
To learn more check out the following examples:
Contributing
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
graphene_mongo-0.4.0.tar.gz
(77.8 kB
view hashes)
Built Distribution
Close
Hashes for graphene_mongo-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f80c185dce33615d7e3fc42c964735ee585685083464f542f90c1013e896fab |
|
MD5 | 569e53c9c360909b5292189d9167f00f |
|
BLAKE2b-256 | c2d34d7ee0929818c4c63a57d0c39f06edb4b2842957ccf8cae561527cf46fe7 |