Skip to content

Django mongoengine example #101

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 4 commits into from
Jul 22, 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
1 change: 1 addition & 0 deletions examples/django_mongoengine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.sqlite3
56 changes: 56 additions & 0 deletions examples/django_mongoengine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

Example Django+MongoEngine Project
================================

This example project demos integration between Graphene, Django 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/django_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
```

Run the following command:

```python
python manage.py migrate
```

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

Start the server:

```python
python manage.py runserver
```

Now head on over to
[http://127.0.0.1:8000/graphql](http://127.0.0.1:8000/graphql)
and run some queries!

For tests run:

```python
pytest -v
```
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions examples/django_mongoengine/bike/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BikeConfig(AppConfig):
name = 'bike'
60 changes: 60 additions & 0 deletions examples/django_mongoengine/bike/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest
from .models import Bike, Shop


def fixture_bike_data():
Bike.drop_collection()
bike_one = Bike(
name='Level R',
brand='Mondraker',
year='2020',
size=['S', 'M', 'L', 'XL'],
wheel_size=27.5,
type='MTB'
)
bike_one.save()

bike_two = Bike(
name='CAADX ULTEGRA',
brand='Cannondale',
year='2019',
size=['46', '51', '54', '58'],
wheel_size=28,
type='Gravel'
)
bike_two.save()

bike_three = Bike(
id="507f1f77bcf86cd799439011",
name='Moterra Neo',
brand='Cannondale',
year='2019',
size=["M", "L", "XL"],
wheel_size=29,
type='EBike'
)
bike_three.save()


def fixture_shop_data():
Shop.drop_collection()
shop_one = Shop(
name="Big Wheel Bicycles",
address="2438 Hart Ridge Road",
website="https://www.bigwheelbike.test"
)
shop_one.save()
shop_two = Shop(
name="Bike Tech",
address="2175 Pearl Street",
website="https://www.biketech.test"
)
shop_two.save()


@pytest.fixture(scope='module')
def fixtures_data():
fixture_bike_data()
fixture_shop_data()

return True
Empty file.
24 changes: 24 additions & 0 deletions examples/django_mongoengine/bike/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from mongoengine import Document
from mongoengine.fields import (
FloatField, StringField,
ListField, URLField, ObjectIdField
)


class Shop(Document):
meta = {'collection': 'shop'}
ID = ObjectIdField()
name = StringField()
address = StringField()
website = URLField()


class Bike(Document):
meta = {'collection': 'bike'}
ID = ObjectIdField()
name = StringField()
brand = StringField()
year = StringField()
size = ListField(StringField())
wheel_size = FloatField()
type = StringField()
80 changes: 80 additions & 0 deletions examples/django_mongoengine/bike/mutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import graphene
from django.core.exceptions import ObjectDoesNotExist
from .models import Bike
from .types import BikeType


class BikeInput(graphene.InputObjectType):
id = graphene.ID()
name = graphene.String()
brand = graphene.String()
year = graphene.String()
size = graphene.List(graphene.String)
wheel_size = graphene.Float()
type = graphene.String()


class CreateBikeMutation(graphene.Mutation):
bike = graphene.Field(BikeType)

class Arguments:
bike_data = BikeInput(required=True)

def mutate(self, info, bike_data=None):
bike = Bike(
name=bike_data.name,
brand=bike_data.brand,
year=bike_data.year,
size=bike_data.size,
wheel_size=bike_data.wheel_size,
type=bike_data.type
)
bike.save()

return CreateBikeMutation(bike=bike)


class UpdateBikeMutation(graphene.Mutation):
bike = graphene.Field(BikeType)

class Arguments:
bike_data = BikeInput(required=True)

@staticmethod
def get_object(id):
return Bike.objects.get(pk=id)

def mutate(self, info, bike_data=None):
bike = UpdateBikeMutation.get_object(bike_data.id)
if bike_data.name:
bike.name = bike_data.name
if bike_data.brand:
bike.brand = bike_data.brand
if bike_data.year:
bike.year = bike_data.year
if bike_data.size:
bike.size = bike_data.size
if bike_data.wheel_size:
bike.wheel_size = bike_data.wheel_size
if bike_data.type:
bike.type = bike_data.type

bike.save()

return UpdateBikeMutation(bike=bike)


class DeleteBikeMutation(graphene.Mutation):
class Arguments:
id = graphene.ID(required=True)

success = graphene.Boolean()

def mutate(self, info, id):
try:
Bike.objects.get(pk=id).delete()
success = True
except ObjectDoesNotExist:
success = False

return DeleteBikeMutation(success=success)
24 changes: 24 additions & 0 deletions examples/django_mongoengine/bike/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import graphene
from graphene.relay import Node
from graphene_mongo.fields import MongoengineConnectionField
from .models import Shop
from .types import BikeType, ShopType
from .mutations import CreateBikeMutation, UpdateBikeMutation, DeleteBikeMutation


class Mutations(graphene.ObjectType):
create_bike = CreateBikeMutation.Field()
update_bike = UpdateBikeMutation.Field()
delete_bike = DeleteBikeMutation.Field()


class Query(graphene.ObjectType):
node = Node.Field()
bikes = MongoengineConnectionField(BikeType)
shop_list = graphene.List(ShopType)

def resolve_shop_list(self, info):
return Shop.objects.all()


schema = graphene.Schema(query=Query, mutation=Mutations, types=[BikeType, ShopType])
Loading