Skip to content

Commit 3617415

Browse files
authored
Merge pull request #102 from eduarde/falcon_mongoengine
Falcon mongoengine
2 parents b018efa + 61d425e commit 3617415

File tree

12 files changed

+412
-0
lines changed

12 files changed

+412
-0
lines changed

examples/falcon_mongoengine/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
Example Falcon+MongoEngine Project
3+
================================
4+
5+
This example project demos integration between Graphene, Falcon and MongoEngine.
6+
7+
Getting started
8+
---------------
9+
10+
First you'll need to get the source of the project. Do this by cloning the
11+
whole Graphene repository:
12+
13+
```bash
14+
# Get the example project code
15+
git clone [email protected]:abawchen/graphene-mongo.git
16+
cd graphene-mongo/examples/falcon_mongoengine
17+
```
18+
19+
Create a virtual environment.
20+
21+
```bash
22+
# Create a virtualenv in which we can install the dependencies
23+
virtualenv env
24+
source env/bin/activate
25+
```
26+
27+
Now we can install our dependencies:
28+
29+
```bash
30+
pip install -r requirements.txt
31+
```
32+
33+
Setup a mongodb connection and create a database.
34+
See the mongoengine connection details in the *app.py* file
35+
36+
Start the server:
37+
38+
On windows:
39+
```
40+
waitress-serve --port=9000 falcon_mongoengine.app:app
41+
```
42+
43+
On Linux:
44+
```
45+
gunicorn -b 0.0.0.0:9000 falcon_mongoengine.app:app
46+
```
47+
48+
Now head on over to
49+
[http://127.0.0.1:9000/graphql?query=](http://127.0.0.1:9000/graphql?query=)
50+
and run some queries!
51+
52+
Example:
53+
54+
```
55+
http://127.0.0.1:9000/graphql?query=query
56+
{
57+
categories(first: 1, name: "Travel")
58+
{
59+
edges { node { name color } }
60+
}
61+
}
62+
```
63+
64+
```
65+
http://127.0.0.1:9000/graphql?query=query
66+
{
67+
bookmarks(first: 10)
68+
{
69+
pageInfo { startCursor endCursor hasNextPage hasPreviousPage }
70+
edges {
71+
node { name url category { name color } tags }
72+
}
73+
}
74+
}
75+
```
76+
77+
For tests run:
78+
79+
```python
80+
pytest -v
81+
```

examples/falcon_mongoengine/__init__.py

Whitespace-only changes.

examples/falcon_mongoengine/api.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import json
2+
import falcon
3+
from .schema import schema
4+
5+
6+
def set_graphql_allow_header(
7+
req: falcon.Request,
8+
resp: falcon.Response,
9+
resource: object,
10+
):
11+
resp.set_header('Allow', 'GET, POST, OPTIONS')
12+
13+
14+
class HelloWorldResource:
15+
16+
def on_get(self, req, resp):
17+
name = "Hello World!"
18+
resp.status = falcon.HTTP_200
19+
resp.body = json.dumps({"respone": name, "status": resp.status})
20+
21+
def on_post(self, req, resp):
22+
pass
23+
24+
25+
@falcon.after(set_graphql_allow_header)
26+
class GraphQLResource:
27+
28+
def on_get(self, req, resp):
29+
query = req.params['query']
30+
result = schema.execute(query)
31+
32+
if result.data:
33+
data_ret = {'data': result.data}
34+
resp.status = falcon.HTTP_200
35+
resp.body = json.dumps(data_ret, separators=(',', ':'))
36+
37+
def on_post(self, req, resp):
38+
query = req.params['query']
39+
result = schema.execute(query)
40+
if result.data:
41+
data_ret = {'data': result.data}
42+
resp.status = falcon.HTTP_200
43+
resp.body = json.dumps(data_ret, separators=(',', ':'))

examples/falcon_mongoengine/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import falcon
2+
from mongoengine import connect
3+
from .api import GraphQLResource, HelloWorldResource
4+
5+
connect('bookmarks_db', host='127.0.0.1', port=27017)
6+
app = application = falcon.API()
7+
8+
helloWorld = HelloWorldResource()
9+
graphQL = GraphQLResource()
10+
11+
app.add_route('/', helloWorld)
12+
app.add_route('/graphql', graphQL)

examples/falcon_mongoengine/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from mongoengine import Document, CASCADE
2+
from mongoengine.fields import StringField, ListField, ReferenceField
3+
4+
5+
class Category(Document):
6+
meta = {'collection': 'category'}
7+
name = StringField(max_length=140, required=True)
8+
color = StringField(max_length=7, required=True)
9+
10+
11+
class Bookmark(Document):
12+
meta = {'collection': 'bookmark'}
13+
name = StringField(required=True)
14+
url = StringField(required=True)
15+
category = ReferenceField('Category', reverse_delete_rule=CASCADE)
16+
tags = ListField(StringField(max_length=50))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
python_files = tests.py test_*.py *_tests.py
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
falcon==2.0.0
2+
mongoengine==0.17.0
3+
graphene-mongo
4+
waitress==1.3.0
5+
pytest==4.6.3
6+
mongomock==3.16.0

examples/falcon_mongoengine/schema.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import graphene
2+
from graphene_mongo.fields import MongoengineConnectionField
3+
from .types import CategoryType, BookmarkType
4+
5+
6+
class Query(graphene.ObjectType):
7+
categories = MongoengineConnectionField(CategoryType)
8+
bookmarks = MongoengineConnectionField(BookmarkType)
9+
10+
11+
schema = graphene.Schema(query=Query, types=[CategoryType, BookmarkType])

examples/falcon_mongoengine/tests/__init__.py

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
from examples.falcon_mongoengine.models import Category, Bookmark
3+
4+
5+
def fixture_category_data():
6+
Category.drop_collection()
7+
category_one = Category(
8+
name='Travel',
9+
color='#ed008c'
10+
)
11+
category_one.save()
12+
13+
category_two = Category(
14+
name='Work',
15+
color='#1769ff'
16+
)
17+
category_two.save()
18+
19+
return category_one, category_two
20+
21+
22+
@pytest.fixture(scope='module')
23+
def fixtures_data():
24+
category_one, category_two = fixture_category_data()
25+
26+
Bookmark.drop_collection()
27+
bookmark_one = Bookmark(
28+
name='Travel tips',
29+
url='https://www.traveltips.test',
30+
category=category_one,
31+
tags=["travel", "tips", "howto", ]
32+
)
33+
bookmark_one.save()
34+
35+
bookmark_two = Bookmark(
36+
name='DIY vacation',
37+
url='https://www.diyvacation.test',
38+
category=category_one,
39+
tags=["travel", "diy", "holiday", "vacation", ]
40+
)
41+
bookmark_two.save()
42+
43+
bookmark_three = Bookmark(
44+
name='Awesome python',
45+
url='https://awesomelists.top/#repos/vinta/awesome-python',
46+
category=category_two,
47+
tags=["python", "dev", "awesome", "tutorial", ]
48+
)
49+
bookmark_three.save()
50+
51+
return True

0 commit comments

Comments
 (0)