|
| 1 | +.. image:: https://travis-ci.org/graphql-python/graphene-mongo.svg?branch=master |
| 2 | + :target: https://travis-ci.org/graphql-python/graphene-mongo |
| 3 | +.. image:: https://coveralls.io/repos/github/graphql-python/graphene-mongo/badge.svg?branch=master |
| 4 | + :target: https://coveralls.io/github/graphql-python/graphene-mongo?branch=master |
| 5 | +.. image:: https://badge.fury.io/py/graphene-mongo.svg |
| 6 | + :target: https://badge.fury.io/py/graphene-mongo |
| 7 | +.. image:: https://img.shields.io/pypi/pyversions/graphene-mongo.svg |
| 8 | + :target: https://pypi.python.org/pypi/graphene-mongo/ |
| 9 | + |
| 10 | +Graphene-Mongo |
| 11 | +============== |
| 12 | + |
| 13 | +A `Mongoengine <https://mongoengine-odm.readthedocs.io/>`__ integration for `Graphene <http://graphene-python.org/>`__. |
| 14 | + |
| 15 | +Installation |
| 16 | +------------ |
| 17 | + |
| 18 | +For instaling graphene-mongo, just run this command in your shell |
| 19 | + |
| 20 | +.. code:: bash |
| 21 | +
|
| 22 | + pip install graphene-mongo |
| 23 | +
|
| 24 | +Examples |
| 25 | +-------- |
| 26 | + |
| 27 | +Here is a simple Mongoengine model as `models.py`: |
| 28 | + |
| 29 | +.. code:: python |
| 30 | +
|
| 31 | + from mongoengine import Document |
| 32 | + from mongoengine.fields import StringField |
| 33 | +
|
| 34 | + class User(Document): |
| 35 | + meta = {'collection': 'user'} |
| 36 | + first_name = StringField(required=True) |
| 37 | + last_name = StringField(required=True) |
| 38 | +
|
| 39 | +
|
| 40 | +To create a GraphQL schema for it you simply have to write the following: |
| 41 | + |
| 42 | +.. code:: python |
| 43 | +
|
| 44 | + import graphene |
| 45 | +
|
| 46 | + from graphene_mongo import MongoengineObjectType |
| 47 | +
|
| 48 | + from .models import User as UserModel |
| 49 | +
|
| 50 | + class User(MongoengineObjectType): |
| 51 | + class Meta: |
| 52 | + model = UserModel |
| 53 | +
|
| 54 | + class Query(graphene.ObjectType): |
| 55 | + users = graphene.List(User) |
| 56 | +
|
| 57 | + def resolve_users(self, info): |
| 58 | + return list(UserModel.objects.all()) |
| 59 | +
|
| 60 | + schema = graphene.Schema(query=Query) |
| 61 | +
|
| 62 | +Then you can simply query the schema: |
| 63 | + |
| 64 | +.. code:: python |
| 65 | +
|
| 66 | + query = ''' |
| 67 | + query { |
| 68 | + users { |
| 69 | + firstName, |
| 70 | + lastName |
| 71 | + } |
| 72 | + } |
| 73 | + ''' |
| 74 | + result = schema.execute(query) |
| 75 | +
|
| 76 | +To learn more check out the `Flask MongoEngine example <https://github.com/graphql-python/graphene-mongo/tree/master/examples/flask_mongoengine>`__ |
| 77 | + |
0 commit comments