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 graphene_mongo.utils 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
Built Distribution
File details
Details for the file graphene_mongo-0.4.4.tar.gz
.
File metadata
- Download URL: graphene_mongo-0.4.4.tar.gz
- Upload date:
- Size: 78.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54da09ab45880e118ac1be1a059dc23cbb3e1398e1a696c808abb2a2c463645b |
|
MD5 | 8970a626c9bf810d7bf318c78acc01fe |
|
BLAKE2b-256 | 573f9df8f5a678a3758dee9123e14ce6938912951634cd65d6626ce10f27620f |
File details
Details for the file graphene_mongo-0.4.4-py3-none-any.whl
.
File metadata
- Download URL: graphene_mongo-0.4.4-py3-none-any.whl
- Upload date:
- Size: 89.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09bb5b57905a48aace5bba3c5d7efca7ae9a09ec3d647c4bd75db09349223e16 |
|
MD5 | 7441bc61139bd911c6cc5e8a4bf3018a |
|
BLAKE2b-256 | 0508f2684ff5ce7b617723369dd81274dbfa411d59614e419e4058f45af587d0 |