Skip to content

Commit 3ac5186

Browse files
committed
Added falcon with mongoengine example
1 parent b018efa commit 3ac5186

File tree

11 files changed

+331
-0
lines changed

11 files changed

+331
-0
lines changed

examples/falcon_mongoengine/__init__.py

Whitespace-only changes.

examples/falcon_mongoengine/api.py

+43
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

+12
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('exampledb', 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

+16
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))
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
python_files = tests.py test_*.py *_tests.py
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

+11
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.
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
+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import mongoengine
2+
from graphene.test import Client
3+
from examples.falcon_mongoengine.schema import schema
4+
from .fixtures import fixtures_data
5+
6+
mongoengine.connect('graphene-mongo-test', host='mongomock://localhost', alias='default')
7+
8+
9+
def test_category_last_1_item_query(fixtures_data):
10+
query = '''
11+
{
12+
categories(last: 1){
13+
edges {
14+
node {
15+
name
16+
color
17+
}
18+
}
19+
}
20+
}'''
21+
22+
expected = {
23+
"data": {
24+
"categories":
25+
{
26+
"edges": [
27+
{
28+
"node": {
29+
"name": "Work",
30+
"color": "#1769ff",
31+
}
32+
}
33+
]
34+
}
35+
}
36+
}
37+
38+
client = Client(schema)
39+
result = client.execute(query)
40+
assert result == expected
41+
42+
43+
def test_category_filter_item_query(fixtures_data):
44+
query = '''
45+
{
46+
categories(name: "Work"){
47+
edges {
48+
node {
49+
name
50+
color
51+
}
52+
}
53+
}
54+
}'''
55+
56+
expected = {
57+
"data": {
58+
"categories":
59+
{
60+
"edges": [
61+
{
62+
"node": {
63+
"name": "Work",
64+
"color": "#1769ff",
65+
}
66+
}
67+
]
68+
}
69+
}
70+
}
71+
72+
client = Client(schema)
73+
result = client.execute(query)
74+
assert result == expected
75+
76+
77+
def test_bookmarks_first_2_items_query(fixtures_data):
78+
query = '''
79+
{
80+
bookmarks(first: 2){
81+
edges {
82+
node {
83+
name
84+
url
85+
category {
86+
name
87+
color
88+
}
89+
tags
90+
}
91+
}
92+
}
93+
}'''
94+
95+
expected = {
96+
"data": {
97+
"bookmarks":
98+
{
99+
"edges": [
100+
{
101+
"node": {
102+
"name": "Travel tips",
103+
"url": "https://www.traveltips.test",
104+
"category": {
105+
'name': 'Travel',
106+
'color': '#ed008c'
107+
},
108+
"tags": ["travel", "tips", "howto", ]
109+
}
110+
},
111+
{
112+
"node": {
113+
"name": "DIY vacation",
114+
"url": "https://www.diyvacation.test",
115+
"category": {
116+
'name': 'Travel',
117+
'color': '#ed008c'
118+
},
119+
"tags": ["travel", "diy", "holiday", "vacation", ]
120+
}
121+
}
122+
]
123+
}
124+
}
125+
}
126+
127+
client = Client(schema)
128+
result = client.execute(query)
129+
assert result == expected
130+
131+
132+
def test_bookmarks_filter_items_query(fixtures_data):
133+
query = '''
134+
{
135+
bookmarks(first: 1, name: "Awesome python"){
136+
edges {
137+
node {
138+
name
139+
url
140+
category {
141+
name
142+
color
143+
}
144+
tags
145+
}
146+
}
147+
}
148+
}'''
149+
150+
expected = {
151+
"data": {
152+
"bookmarks":
153+
{
154+
"edges": [
155+
{
156+
"node": {
157+
"name": "Awesome python",
158+
"url": "https://awesomelists.top/#repos/vinta/awesome-python",
159+
"category": {
160+
'name': 'Work',
161+
'color': '#1769ff'
162+
},
163+
"tags": ["python", "dev", "awesome", "tutorial", ]
164+
}
165+
}
166+
]
167+
}
168+
}
169+
}
170+
171+
client = Client(schema)
172+
result = client.execute(query)
173+
assert result == expected

examples/falcon_mongoengine/types.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import graphene
2+
from graphene import relay
3+
from graphene_mongo import MongoengineObjectType
4+
5+
from .models import Category, Bookmark
6+
7+
8+
class CategoryType(MongoengineObjectType):
9+
class Meta:
10+
model = Category
11+
interfaces = (relay.Node,)
12+
13+
14+
class BookmarkType(MongoengineObjectType):
15+
class Meta:
16+
model = Bookmark
17+
interfaces = (relay.Node,)

0 commit comments

Comments
 (0)