Skip to main content

Graphene Mongoengine integration

Project description

Build Status Coverage Status Documentation Status PyPI version PyPI pyversions Downloads

Lint Test Package

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


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.2.tar.gz (78.3 kB view details)

Uploaded Source

Built Distribution

graphene_mongo-0.4.2-py3-none-any.whl (89.9 kB view details)

Uploaded Python 3

File details

Details for the file graphene_mongo-0.4.2.tar.gz.

File metadata

  • Download URL: graphene_mongo-0.4.2.tar.gz
  • Upload date:
  • Size: 78.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for graphene_mongo-0.4.2.tar.gz
Algorithm Hash digest
SHA256 8b112135d1a6a5403a54f18149d34f8e8ef557306c55f4337336a078857d7707
MD5 29530049b9d8c38e2365f07da092b461
BLAKE2b-256 f286ee2c233378234b44ca26b9622ea72bc6c0ea92fb209d95efa885fea14856

See more details on using hashes here.

Provenance

File details

Details for the file graphene_mongo-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for graphene_mongo-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 32700b2bc366ef0dc685fbcdccac9753c7205eecf54e90a99eb9bb4c943a50fb
MD5 a9b66fbfd2b50d5b07a4db447f5f3153
BLAKE2b-256 313995a837ce5fd8b4376f72bb08981c13df844583bf784120518e5f352f20fc

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page