Skip to content

Commit 00d8b22

Browse files
committed
Added django_mongoengine example app
1 parent dfe18a0 commit 00d8b22

File tree

20 files changed

+425
-0
lines changed

20 files changed

+425
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
db.sqlite3

examples/django_mongoengine/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
Example Django+MongoEngine Project
3+
================================
4+
5+
This example project demos integration between Graphene, Django 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/django_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+
Run the following command:
34+
35+
```python
36+
python manage.py migrate
37+
```
38+
39+
Setup a mongodb connection and create a database.
40+
See the mongoengine connection details in the *settings.py* file
41+
42+
Start the server:
43+
44+
```python
45+
python manage.py runserver
46+
```
47+
48+
Now head on over to
49+
[http://127.0.0.1:8000/graphql](http://127.0.0.1:8000/graphql)
50+
and run some queries!
51+
52+
For tests run:
53+
54+
```python
55+
pytest -v
56+
```

examples/django_mongoengine/__init__.py

Whitespace-only changes.

examples/django_mongoengine/bike/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class BikeConfig(AppConfig):
5+
name = 'bike'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from .models import Bike
3+
4+
5+
@pytest.fixture(scope='module')
6+
def fixtures_data():
7+
Bike.drop_collection()
8+
bike_one = Bike(
9+
name='Level R',
10+
brand='Mondraker',
11+
year='2020',
12+
size=['S', 'M', 'L', 'XL'],
13+
wheel_size=27.5,
14+
type='MTB'
15+
)
16+
bike_one.save()
17+
18+
bike_two = Bike(
19+
name='CAADX ULTEGRA',
20+
brand='Cannondale',
21+
year='2019',
22+
size=['46', '51', '54', '58'],
23+
wheel_size=28,
24+
type='Gravel'
25+
)
26+
bike_two.save()
27+
28+
return True

examples/django_mongoengine/bike/migrations/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mongoengine import Document
2+
from mongoengine.fields import FloatField, StringField, ListField
3+
4+
5+
class Bike(Document):
6+
meta = {'collection': 'bike'}
7+
name = StringField()
8+
brand = StringField()
9+
year = StringField()
10+
size = ListField(StringField())
11+
wheel_size = FloatField()
12+
type = StringField()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import graphene
2+
from graphene.relay import Node
3+
from graphene_mongo.fields import MongoengineConnectionField
4+
from .types import BikeType
5+
6+
7+
class Query(graphene.ObjectType):
8+
node = Node.Field()
9+
bikes = MongoengineConnectionField(BikeType)
10+
11+
12+
schema = graphene.Schema(query=Query, types=[BikeType, ])
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from graphene.test import Client
2+
from .schema import schema
3+
from .fixtures import fixtures_data
4+
5+
6+
def test_bikes_last_item_query(fixtures_data):
7+
query = '''
8+
{
9+
bikes(last: 1){
10+
edges {
11+
node {
12+
name
13+
brand
14+
year
15+
size
16+
wheelSize
17+
type
18+
}
19+
}
20+
}
21+
}'''
22+
23+
expected = {
24+
"data": {
25+
"bikes":
26+
{
27+
"edges": [
28+
{
29+
"node": {
30+
"name": "CAADX ULTEGRA",
31+
"brand": "Cannondale",
32+
"year": '2019',
33+
"size": ['46', '51', '54', '58'],
34+
"wheelSize": 28,
35+
"type": "Gravel"
36+
}
37+
},
38+
]
39+
}
40+
}
41+
}
42+
43+
client = Client(schema)
44+
result = client.execute(query)
45+
assert result == expected
46+
47+
48+
def test_bikes_filter_by_type_item_query(fixtures_data):
49+
query = '''
50+
{
51+
bikes(first: 2, type: "MTB"){
52+
edges {
53+
node {
54+
name
55+
brand
56+
year
57+
size
58+
wheelSize
59+
type
60+
}
61+
}
62+
}
63+
}'''
64+
65+
expected = {
66+
"data": {
67+
"bikes":
68+
{
69+
"edges": [
70+
{
71+
"node": {
72+
"name": "Level R",
73+
"brand": "Mondraker",
74+
"year": '2020',
75+
"size": ['S', 'M', 'L', 'XL'],
76+
"wheelSize": 27.5,
77+
"type": "MTB"
78+
}
79+
},
80+
]
81+
}
82+
}
83+
}
84+
85+
client = Client(schema)
86+
result = client.execute(query)
87+
assert result == expected

0 commit comments

Comments
 (0)