This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Django에서 Graphene과 Mongoengine 기본 틀 만들기
Sean Hong(홍성민) edited this page Aug 10, 2021
·
1 revision
Django에서 Python용 GraphQL 라이브러리인 Graphene을 추가하는 방법입니다.
pip install graphene graphene-mongo graphene-django mongoengine
django-admin startproject backend
cd backend
python manage.py startapp api
INSTALLED_APPS = [
...
'api',
'graphene_django',
'graphene_mongo'
]
...
...
# Mongoengine 연결
from mongoengine import connect
connect(db=DB_NAME, host=MONGO_HOST, port=27017, username=USERNAME, password=PASSWORD, authentication_source='admin')
# GraphQL Schema 파일
GRAPHENE = {
"SCHEMA": "api.schema.schema",
}
GraphQL endpoint url 을 생성해 줍니다.
...
from graphene_django.views import GraphQLView
from django.views.decorators.csrf import csrf_exempt
urlpatterns = [
path('admin/', admin.site.urls),
path('graphql', csrf_exempt(GraphQLView.as_view(graphiql=True))),
]
MongoDB의 형태(스키마)에 대한 GraphQL 쿼리를 생성해줍니다.
import graphene
from graphene.relay import Node
from graphene_mongo import MongoengineConnectionField, MongoengineObjectType
from models import Company as CompanyModel
from models import Tool as ToolModel
class Companies(MongoengineObjectType):
class Meta:
model = CompanyModel
interfaces = (Node,)
class Tools(MongoengineObjectType):
class Meta:
model = ToolModel
interfaces = (Node,)
class Query(graphene.ObjectType):
node = Node.Field()
all_companies = MongoengineConnectionField(Companies)
all_tools = MongoengineConnectionField(Tools)
schema = graphene.Schema(query=Query, types=[Companies, Tools])
GraphQL 스키마를 만들기 위한 MongoDB 모델을 명시해 줍니다.
from django.db import models
from mongoengine import Document
from mongoengine.fields import (
StringField,
URLField,
ListField,
)
# Create your models here.
class Company(Document):
# MongoDB Collection 이름
meta = {"collection": "companies"}
name = StringField()
description = StringField()
url = URLField()
class Tool(Document):
# MongoDB Collection 이름
meta = {"collection": "tools"}
name = StringField()
description = StringField()
- dummy_tools.json
[
{
"name": "MongoDB",
"description": "NoSQL database"
},
{
"name": "MySQL",
"description": "SQL database"
}
]
- dummy_companies.json
[
{
"name": "Coupang",
"description": "Korean Amazon"
},
{
"name": "Tesla",
"description": "E-car company"
}
]
import json
import pymongo
comp = json.load(fp=open("dummy_companies.json"))
tool = json.load(fp=open("dummy_tools.json"))
dbs = {
"MONGODB_SERVER": "localhost",
"MONGODB_PORT": 27017,
"USERNAME": "your-mongo-username",
"PASSWORD": "your-mongo-password",
"MONGODB_DB": "your-mongo-db",
"MONGODB_COLLECTION": "your-mongo-collection"
}
connection = MongoClient(
host=dbs["MONGODB_SERVER"],
port=dbs["MONGODB_PORT"],
username=dbs["USERNAME"],
password=dbs["PASSWORD"],
)
db = connection[dbs["MONGODB_DB"]]
collection = db[dbs["MONGODB_COLLECTION"]]
collection.insert_many(comp)
collection.insert_many(tool)
python manage.py rumserver