Skip to content

Commit b018efa

Browse files
authored
Merge pull request #101 from eduarde/django_mongoengine_example
Django mongoengine example
2 parents dfe18a0 + a2b27af commit b018efa

21 files changed

+721
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import pytest
2+
from .models import Bike, Shop
3+
4+
5+
def fixture_bike_data():
6+
Bike.drop_collection()
7+
bike_one = Bike(
8+
name='Level R',
9+
brand='Mondraker',
10+
year='2020',
11+
size=['S', 'M', 'L', 'XL'],
12+
wheel_size=27.5,
13+
type='MTB'
14+
)
15+
bike_one.save()
16+
17+
bike_two = Bike(
18+
name='CAADX ULTEGRA',
19+
brand='Cannondale',
20+
year='2019',
21+
size=['46', '51', '54', '58'],
22+
wheel_size=28,
23+
type='Gravel'
24+
)
25+
bike_two.save()
26+
27+
bike_three = Bike(
28+
id="507f1f77bcf86cd799439011",
29+
name='Moterra Neo',
30+
brand='Cannondale',
31+
year='2019',
32+
size=["M", "L", "XL"],
33+
wheel_size=29,
34+
type='EBike'
35+
)
36+
bike_three.save()
37+
38+
39+
def fixture_shop_data():
40+
Shop.drop_collection()
41+
shop_one = Shop(
42+
name="Big Wheel Bicycles",
43+
address="2438 Hart Ridge Road",
44+
website="https://www.bigwheelbike.test"
45+
)
46+
shop_one.save()
47+
shop_two = Shop(
48+
name="Bike Tech",
49+
address="2175 Pearl Street",
50+
website="https://www.biketech.test"
51+
)
52+
shop_two.save()
53+
54+
55+
@pytest.fixture(scope='module')
56+
def fixtures_data():
57+
fixture_bike_data()
58+
fixture_shop_data()
59+
60+
return True

examples/django_mongoengine/bike/migrations/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from mongoengine import Document
2+
from mongoengine.fields import (
3+
FloatField, StringField,
4+
ListField, URLField, ObjectIdField
5+
)
6+
7+
8+
class Shop(Document):
9+
meta = {'collection': 'shop'}
10+
ID = ObjectIdField()
11+
name = StringField()
12+
address = StringField()
13+
website = URLField()
14+
15+
16+
class Bike(Document):
17+
meta = {'collection': 'bike'}
18+
ID = ObjectIdField()
19+
name = StringField()
20+
brand = StringField()
21+
year = StringField()
22+
size = ListField(StringField())
23+
wheel_size = FloatField()
24+
type = StringField()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import graphene
2+
from django.core.exceptions import ObjectDoesNotExist
3+
from .models import Bike
4+
from .types import BikeType
5+
6+
7+
class BikeInput(graphene.InputObjectType):
8+
id = graphene.ID()
9+
name = graphene.String()
10+
brand = graphene.String()
11+
year = graphene.String()
12+
size = graphene.List(graphene.String)
13+
wheel_size = graphene.Float()
14+
type = graphene.String()
15+
16+
17+
class CreateBikeMutation(graphene.Mutation):
18+
bike = graphene.Field(BikeType)
19+
20+
class Arguments:
21+
bike_data = BikeInput(required=True)
22+
23+
def mutate(self, info, bike_data=None):
24+
bike = Bike(
25+
name=bike_data.name,
26+
brand=bike_data.brand,
27+
year=bike_data.year,
28+
size=bike_data.size,
29+
wheel_size=bike_data.wheel_size,
30+
type=bike_data.type
31+
)
32+
bike.save()
33+
34+
return CreateBikeMutation(bike=bike)
35+
36+
37+
class UpdateBikeMutation(graphene.Mutation):
38+
bike = graphene.Field(BikeType)
39+
40+
class Arguments:
41+
bike_data = BikeInput(required=True)
42+
43+
@staticmethod
44+
def get_object(id):
45+
return Bike.objects.get(pk=id)
46+
47+
def mutate(self, info, bike_data=None):
48+
bike = UpdateBikeMutation.get_object(bike_data.id)
49+
if bike_data.name:
50+
bike.name = bike_data.name
51+
if bike_data.brand:
52+
bike.brand = bike_data.brand
53+
if bike_data.year:
54+
bike.year = bike_data.year
55+
if bike_data.size:
56+
bike.size = bike_data.size
57+
if bike_data.wheel_size:
58+
bike.wheel_size = bike_data.wheel_size
59+
if bike_data.type:
60+
bike.type = bike_data.type
61+
62+
bike.save()
63+
64+
return UpdateBikeMutation(bike=bike)
65+
66+
67+
class DeleteBikeMutation(graphene.Mutation):
68+
class Arguments:
69+
id = graphene.ID(required=True)
70+
71+
success = graphene.Boolean()
72+
73+
def mutate(self, info, id):
74+
try:
75+
Bike.objects.get(pk=id).delete()
76+
success = True
77+
except ObjectDoesNotExist:
78+
success = False
79+
80+
return DeleteBikeMutation(success=success)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import graphene
2+
from graphene.relay import Node
3+
from graphene_mongo.fields import MongoengineConnectionField
4+
from .models import Shop
5+
from .types import BikeType, ShopType
6+
from .mutations import CreateBikeMutation, UpdateBikeMutation, DeleteBikeMutation
7+
8+
9+
class Mutations(graphene.ObjectType):
10+
create_bike = CreateBikeMutation.Field()
11+
update_bike = UpdateBikeMutation.Field()
12+
delete_bike = DeleteBikeMutation.Field()
13+
14+
15+
class Query(graphene.ObjectType):
16+
node = Node.Field()
17+
bikes = MongoengineConnectionField(BikeType)
18+
shop_list = graphene.List(ShopType)
19+
20+
def resolve_shop_list(self, info):
21+
return Shop.objects.all()
22+
23+
24+
schema = graphene.Schema(query=Query, mutation=Mutations, types=[BikeType, ShopType])

0 commit comments

Comments
 (0)