사용자 도구

사이트 도구


python:django:graphene

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
python:django:graphene [2024/11/13 12:07] – [Graphene] taekgupython:django:graphene [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 +====== Graphene ======
 +  * [[https://docs.graphene-python.org/projects/django/en/latest/|Graphene-Django]]
 +
 +Graphene-Django는 Django 프로젝트에 GraphQL 기능을 쉽게 추가할 수 있는 몇 가지 추가 추상화를 제공합니다.
 +===== graphene-django =====
 +==== Install ====
 +<code bash>
 +pip install graphene-django
 +</code>
 +
 +INSTALLED_APPS에 추가
 +<code python setting.py>
 +INSTALLED_APPS = [
 +    ...
 +    "django.contrib.staticfiles", # Required for GraphiQL
 +    "graphene_django"
 +]
 +</code>
 +
 +URL추가
 +<code python urls.py>
 +from django.urls import path
 +from graphene_django.views import GraphQLView
 +
 +urlpatterns = [
 +    # ...
 +    path("graphql", GraphQLView.as_view(graphiql=True)),
 +]
 +</code>
 +(Change graphiql=True to graphiql=False if you do not want to use the GraphiQL API browser.)
 +
 +schema위치를 지정
 +<code python settings.py>
 +GRAPHENE = {
 +    "SCHEMA": "django_root.schema.schema"
 +}
 +</code>
 +
 +**path.schema.schema**는 Django프로젝트에서 Schema오브젝트의 위치이다.
 +기존 schema.py는:
 +<code python schema.py>
 +import graphene
 +
 +class Query(graphene.ObjectType):
 +    hello = graphene.String(default_value="Hi!")
 +
 +schema = graphene.Schema(query=Query)
 +</code>
 +
 +==== CSRF exempt ====
 +Django 앱에서 CSRF 보호 를 활성화 했다면 API 클라이언트가 graphql엔드포인트에 POST하는 것을 방지한다는 것을 알게 될 것입니다 . API 클라이언트를 업데이트하여 각 요청과 함께 CSRF 토큰을 전달할 수 있습니다(Django 문서에는 이를 수행하는 방법에 대한 가이드가 있습니다: https://docs.djangoproject.com/en/3.0/ref/csrf/#ajax ). 당신은 포장하여 CSRF 보호에서 Graphql 엔드 포인트를 면제 할 수있다 GraphQLView로 csrf_exempt 장식 :
 +
 +<code python urls.py>
 +from django.urls import path
 +from django.views.decorators.csrf import csrf_exempt
 +
 +from graphene_django.views import GraphQLView
 +
 +urlpatterns = [
 +    # ...
 +    path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True))),
 +]
 +</code>