Mongodb + GraphQL + Django
Mongodb installation
docker pull mongo
docker run --name mongodb mongo
docker ps
================================
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fef936de7f9 mongo "docker-entrypoint.s" 10 days ago Up About an hour 0.0.0.0:27017->27017/tcp mongodb
Mongodb Compass
Django Constructor
conda update conda
conda create --name NGDP python=3.6
activate NGDP
(NGDP) D:\NGDP>
conda install -y Django=3.1.2
conda install -y -c conda-forge django-cors-headers
conda install -y -c conda-forge graphene-django
conda install -y -c conda-forge graphene-mongo
conda install -y -c conda-forge django-filter
conda install -y -c conda-forge mongoengine
conda install -y -c conda-forge mongomock
conda install -c conda-forge neo4jdjango
conda install pytest
conda install pytest-django
django-admin startproject ngdp
conda install pip
Start Project
django-admin startproject shoesstore
cd shoesstore
django-admin startapp store
django-admin startapp shoes
Start Programming (Backend)
Project Structure
shoesstore/settings.py
# 載入 mongodb 引擎
from mongoengine import connect
# 允許所有裝置連線
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 預載程式
'graphene_django',
'graphene_mongo',
'store',
'shoes'
]
# mongodb connection
_MONGODB_USER = ""
_MONGODB_PASSWD = ""
_MONGODB_HOST = "localhost"
_MONGODB_NAME = "shoesstore"
_MONGODB_PORT = 27017
_MONGODB_DATABASE_HOST = "mongodb://%s:%s@%s/%s" % (
_MONGODB_USER,
_MONGODB_PASSWD,
_MONGODB_HOST,
_MONGODB_NAME,
)
connect(_MONGODB_NAME, host=_MONGODB_HOST, port=_MONGODB_PORT)
# Graphql Schema
GRAPHENE = {"SCHEMA": "shoesstore.schema.schema"}
shoesstore/urls.py
# 增加 graphql 路徑,csrf_exempt 跨網站請求豁免
urlpatterns = [
path('admin/', admin.site.urls),
path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True)), name="graphql-query")
]
Graphql
query ($name: String) {
bikes(first:1, name_Istartswith: $name) {
totalCount
pageInfo {
hasPreviousPage
hasNextPage
}
edges {
node {
name
brand
year
size
wheelSize
type
}
}
}
}
query {
allCategories(first: 1, after: "opaqueCursor") {
edges {
node {
name,
ingredients {
edges {
node {
name
}
}
}
}
}
pageInfo {
hasNextPage
}
}
}
{
shoes(first: 2) {
totalCount
pageInfo {
startCursor
hasNextPage
hasPreviousPage
endCursor
}
edges {
cursor
node {
name
brand
model
id
}
}
}
}
query ($name: String) {
bikes(first:2, name_Istartswith: $name, after: "YXJyYXljb25uZWN0aW9uOjE=") {
totalCount
pageInfo {
endCursor
hasPreviousPage
hasNextPage
}
edges {
node {
id
name
brand
year
size
wheelSize
type
}
cursor
}
}
}
React Native App
expo init ShoesStoreProjectR
Top comments (5)
Hi, I have a collection in MongoDb(locahost). How should I proceed with connecting the collections data in django with graphql?
Hi,
Step 1.
You need to create model for your collection .
Step 2.
Using graphene is a GraphQL API library for Python create a type for your collection.
Step 3.
Delcare your type for graphene schema.
That's the roughly process of using graphql with mongodb thank you!
Thanks @hyperredstart ! I just had one more question-
Will Step 2 code be in schema.py or models.py?
Hi @shlokabhalgat
Step 2 is separate from schema.py, As you wish you could place TypeObject code in the schema.py.
Thanks!
Thanks a lot @hyperredstart I was able to do it! Keep up the good work😁