iT邦幫忙

1

Steps for Django GraphQL -- Set up

GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data.

  1. 安裝 django graphene
    pip install "graphene-django>=2.0"
  1. 創建新的子專案 graphAPI
    python manage.py startapp graphAPI
  1. 至總管理專案 djangoAPI 新增路由 (urls)
from django.conf.urls import url, include
from django.contrib import admin
# 新增以下兩行 -------------------------------
from graphene_django.views import GraphQLView
from graphAPI.schema import schema
# -------------------------------------------

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/', include('api.urls')),
# 新增以下 -------------------------------
    url(r'^graph/', GraphQLView.as_view(graphiql=True, schema=schema))
# ---------------------------------------
]
  1. 至子專案 graphAPI 創建檔案 schema.py (我們晚點再回來處理它)
  1. 至總管理專案 djangoAPI 新增設定 (settings)
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api',
    #  'corsheaders',
# 新增以下 -----------------------
    'graphAPI',
    'graphene_django'
#--------------------------------
]

# 新增以下 -----------------------
GRAPHENE = {
    'SCHEMA': 'graphAPI.schema' # Where your Graphene schema lives
}
#--------------------------------

至此就完成主要設定囉 ^-^


尚未有邦友留言

立即登入留言