Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/graphql-python/graphene-mongo
Graphene MongoEngine integration
https://github.com/graphql-python/graphene-mongo
graphene graphene-mongo graphql mongoengine
Last synced: 28 days ago
JSON representation
Graphene MongoEngine integration
- Host: GitHub
- URL: https://github.com/graphql-python/graphene-mongo
- Owner: graphql-python
- License: mit
- Created: 2018-01-22T04:06:16.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-15T19:37:00.000Z (4 months ago)
- Last Synced: 2024-09-28T12:21:46.322Z (about 1 month ago)
- Topics: graphene, graphene-mongo, graphql, mongoengine
- Language: Python
- Homepage: http://graphene-mongo.readthedocs.io/en/latest/
- Size: 650 KB
- Stars: 288
- Watchers: 14
- Forks: 113
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-starred-test - graphql-python/graphene-mongo - Graphene MongoEngine integration (Python)
- best-of-web-python - GitHub - 22% open · ⏱️ 23.03.2024): (GraphQL Utilities)
README
[![Build Status](https://travis-ci.org/graphql-python/graphene-mongo.svg?branch=master)](https://travis-ci.org/graphql-python/graphene-mongo) [![Coverage Status](https://coveralls.io/repos/github/graphql-python/graphene-mongo/badge.svg?branch=master)](https://coveralls.io/github/graphql-python/graphene-mongo?branch=master) [![Documentation Status](https://readthedocs.org/projects/graphene-mongo/badge/?version=latest)](http://graphene-mongo.readthedocs.io/en/latest/?badge=latest) [![PyPI version](https://badge.fury.io/py/graphene-mongo.svg)](https://badge.fury.io/py/graphene-mongo) [![PyPI pyversions](https://img.shields.io/pypi/pyversions/graphene-mongo.svg)](https://pypi.python.org/pypi/graphene-mongo/) [![Downloads](https://pepy.tech/badge/graphene-mongo)](https://pepy.tech/project/graphene-mongo)
[![Lint](https://github.com/graphql-python/graphene-mongo/actions/workflows/lint.yml/badge.svg?branch=master)](https://github.com/graphql-python/graphene-mongo/actions/workflows/lint.yml) [![Test Package](https://github.com/graphql-python/graphene-mongo/actions/workflows/ci.yml/badge.svg)](https://github.com/graphql-python/graphene-mongo/actions/workflows/ci.yml)
# Graphene-Mongo
A [Mongoengine](https://mongoengine-odm.readthedocs.io/) integration for [Graphene](http://graphene-python.org/).
## 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`:
```python
from mongoengine import Document
from mongoengine.fields import StringFieldclass 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:
```python
import graphenefrom graphene_mongo import MongoengineObjectType
from .models import User as UserModel
class User(MongoengineObjectType):
class Meta:
model = UserModelclass 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:
```python
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:
```python
import graphenefrom graphene_mongo import AsyncMongoengineObjectType
from graphene_mongo.utils import sync_to_async
from concurrent.futures import ThreadPoolExecutorfrom .models import User as UserModel
class User(AsyncMongoengineObjectType):
class Meta:
model = UserModelclass 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:
```python
query = '''
query {
users {
firstName,
lastName
}
}
'''
result = await schema.execute_async(query)
```To learn more check out the following [examples](examples/):
* [Flask MongoEngine example](examples/flask_mongoengine)
* [Django MongoEngine example](examples/django_mongoengine)
* [Falcon MongoEngine example](examples/falcon_mongoengine)## Contributing
After cloning this repo, ensure dependencies are installed by running:
```sh
pip install -r requirements.txt
```After developing, the full test suite can be evaluated by running:
```sh
make test
```