Graphene 에는 Django 작업을 정말 간단하게 만들 수 있도록 설계된 여러 추가 기능이 있습니다.
https://docs.graphene-python.org/projects/django/en/latest/tutorial-relay/
부분에서 basic 가 크게 다른 부분이 없었다.
all_ingredients 를 통해서 모든 ingredients 를 제공해줄수있고 ,
ingredient 를 통해서 특정한 데이터를 얻을 수가 있다.
/ingredients/schema.py
import graphene
from graphene import relay , ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from ingredients.models import Category , Ingredient
class CategoryNode(DjangoObjectType):
class Meta:
model = Category
filter_fields = ['name' , 'ingredients']
interfaces = (relay.Node,)
class IngredientNode(DjangoObjectType):
class Meta:
model = Ingredient
# Allow for some more advanced filtering here
filter_fields = {
'name' : ['exact' , 'icontains' , 'istartswith'],
'notes' : ['exact' , 'icontains'],
'category' : ['exact'],
'category__name' : ['exact'],
}
interfaces = (relay.Node,)
class Query(graphene.ObjectType):
categpry = relay.Node.Field(CategoryNode)
all_categories = DjangoFilterConnectionField(CategoryNode)
ingredient = relay.Node.Field(IngredientNode)
all_ingredients = DjangoFilterConnectionField(IngredientNode)
filtering 기능은 django_filter 에 의해 제공되어진다.
https://django-filter.readthedocs.io/en/latest/guide/usage.html#the-filter
위의 url 에 filter 에 대한 사용 문서가 나와있는데 위의 url 을 참고하면 된다.
pip install django-filter
부모 레벨의 schema.py 를 만들어야한다
프로젝트/schema.py
import graphene
import ingredients.schema
class Query(ingredients.schema.Query , graphene.ObjectType):
# This class will inherit from multiple Queries
# as we begin to add more apps to our project
pass
schema = graphene.Schema(query = Query)
REST API 를 작성하게 될때 ,
프로젝트/urls.py 에서
path("users" , contains("users.urls"),
path("review" , contains("review.urls"),
역할과 비슷하다고 볼수있다.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ingredients',
"graphene_django",
]
GRAPHENE = {
"SCHEMA" : "graphql_study.schema.schema"
}
GraphQL 은 REST API 와 다르게 GraphQL 이 접근되어지는 URL 이 오직 하나만 존재한다.
요청은 하나 존재하는 URL 로 가게되고 ,
Graphene's 에 있는 GraphQLView 로써 다뤄진다.
graphiql=True 를 해서 파라미터로 지정하게되면 ,
from django.conf.urls import url, include
from django.contrib import admin
from graphene_django.views import GraphQLView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^graphql$', GraphQLView.as_view(graphiql=True)),
]
이 view 는 우리가 GraphiQL을 원할때 , GraphQL 엔드포인트 로 제공되어진다.
아마 프로젝트/urls.py 부분이
from django.contrib import admin
from django.urls import path
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView
from graphql_study.schema import schema
urlpatterns = [
path('admin/', admin.site.urls),
path("graphql" , csrf_exempt(GraphQLView.as_view(graphiql=True , schema=schema))),
]
이렇게 되어있을것인데
밑에 처럼 바꿔봅니다.
from django.conf.urls import url , include
from django.contrib import admin
from graphene_django.views import GraphQLView
urlpatterns = [
url(r'^admin/' , admin.site.urls),
url(r'^graphql$' , GraphQLView.as_view(graphiql=True)),
]
로 접속해서 확인을 해본다.
https://docs.graphene-python.org/projects/django/en/latest/tutorial-relay/