Skip to content

Falcon mongoengine #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions examples/falcon_mongoengine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

Example Falcon+MongoEngine Project
================================

This example project demos integration between Graphene, Falcon and MongoEngine.

Getting started
---------------

First you'll need to get the source of the project. Do this by cloning the
whole Graphene repository:

```bash
# Get the example project code
git clone [email protected]:abawchen/graphene-mongo.git
cd graphene-mongo/examples/falcon_mongoengine
```

Create a virtual environment.

```bash
# Create a virtualenv in which we can install the dependencies
virtualenv env
source env/bin/activate
```

Now we can install our dependencies:

```bash
pip install -r requirements.txt
```

Setup a mongodb connection and create a database.
See the mongoengine connection details in the *app.py* file

Start the server:

On windows:
```
waitress-serve --port=9000 falcon_mongoengine.app:app
```

On Linux:
```
gunicorn -b 0.0.0.0:9000 falcon_mongoengine.app:app
```

Now head on over to
[http://127.0.0.1:9000/graphql?query=](http://127.0.0.1:9000/graphql?query=)
and run some queries!

Example:

```
http://127.0.0.1:9000/graphql?query=query
{
categories(first: 1, name: "Travel")
{
edges { node { name color } }
}
}
```

```
http://127.0.0.1:9000/graphql?query=query
{
bookmarks(first: 10)
{
pageInfo { startCursor endCursor hasNextPage hasPreviousPage }
edges {
node { name url category { name color } tags }
}
}
}
```

For tests run:

```python
pytest -v
```
Empty file.
43 changes: 43 additions & 0 deletions examples/falcon_mongoengine/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
import falcon
from .schema import schema


def set_graphql_allow_header(
req: falcon.Request,
resp: falcon.Response,
resource: object,
):
resp.set_header('Allow', 'GET, POST, OPTIONS')


class HelloWorldResource:

def on_get(self, req, resp):
name = "Hello World!"
resp.status = falcon.HTTP_200
resp.body = json.dumps({"respone": name, "status": resp.status})

def on_post(self, req, resp):
pass


@falcon.after(set_graphql_allow_header)
class GraphQLResource:

def on_get(self, req, resp):
query = req.params['query']
result = schema.execute(query)

if result.data:
data_ret = {'data': result.data}
resp.status = falcon.HTTP_200
resp.body = json.dumps(data_ret, separators=(',', ':'))

def on_post(self, req, resp):
query = req.params['query']
result = schema.execute(query)
if result.data:
data_ret = {'data': result.data}
resp.status = falcon.HTTP_200
resp.body = json.dumps(data_ret, separators=(',', ':'))
12 changes: 12 additions & 0 deletions examples/falcon_mongoengine/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import falcon
from mongoengine import connect
from .api import GraphQLResource, HelloWorldResource

connect('bookmarks_db', host='127.0.0.1', port=27017)
app = application = falcon.API()

helloWorld = HelloWorldResource()
graphQL = GraphQLResource()

app.add_route('/', helloWorld)
app.add_route('/graphql', graphQL)
16 changes: 16 additions & 0 deletions examples/falcon_mongoengine/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from mongoengine import Document, CASCADE
from mongoengine.fields import StringField, ListField, ReferenceField


class Category(Document):
meta = {'collection': 'category'}
name = StringField(max_length=140, required=True)
color = StringField(max_length=7, required=True)


class Bookmark(Document):
meta = {'collection': 'bookmark'}
name = StringField(required=True)
url = StringField(required=True)
category = ReferenceField('Category', reverse_delete_rule=CASCADE)
tags = ListField(StringField(max_length=50))
2 changes: 2 additions & 0 deletions examples/falcon_mongoengine/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
python_files = tests.py test_*.py *_tests.py
6 changes: 6 additions & 0 deletions examples/falcon_mongoengine/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
falcon==2.0.0
mongoengine==0.17.0
graphene-mongo
waitress==1.3.0
pytest==4.6.3
mongomock==3.16.0
11 changes: 11 additions & 0 deletions examples/falcon_mongoengine/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import graphene
from graphene_mongo.fields import MongoengineConnectionField
from .types import CategoryType, BookmarkType


class Query(graphene.ObjectType):
categories = MongoengineConnectionField(CategoryType)
bookmarks = MongoengineConnectionField(BookmarkType)


schema = graphene.Schema(query=Query, types=[CategoryType, BookmarkType])
Empty file.
51 changes: 51 additions & 0 deletions examples/falcon_mongoengine/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from examples.falcon_mongoengine.models import Category, Bookmark


def fixture_category_data():
Category.drop_collection()
category_one = Category(
name='Travel',
color='#ed008c'
)
category_one.save()

category_two = Category(
name='Work',
color='#1769ff'
)
category_two.save()

return category_one, category_two


@pytest.fixture(scope='module')
def fixtures_data():
category_one, category_two = fixture_category_data()

Bookmark.drop_collection()
bookmark_one = Bookmark(
name='Travel tips',
url='https://www.traveltips.test',
category=category_one,
tags=["travel", "tips", "howto", ]
)
bookmark_one.save()

bookmark_two = Bookmark(
name='DIY vacation',
url='https://www.diyvacation.test',
category=category_one,
tags=["travel", "diy", "holiday", "vacation", ]
)
bookmark_two.save()

bookmark_three = Bookmark(
name='Awesome python',
url='https://awesomelists.top/#repos/vinta/awesome-python',
category=category_two,
tags=["python", "dev", "awesome", "tutorial", ]
)
bookmark_three.save()

return True
Loading