Post

Django에 GraphQL 구현하기

Django 프로젝트에 GraphQL 설정하기

1. 라이브러리 설치

1
   pip install graphene-django

2. 설치된 앱에 Graphene-Django 추가

settings.py 수정

1
2
3
4
5
6
7
8
   INSTALLED_APPS = [
       ...
       'graphene_django',
   ]
   
   GRAPHENE = {
       'SCHEMA': 'yourapp.schema.schema',  # 스키마가 정의된 곳
   }

3. GraphQL 스키마 작성

schema.py 파일에 GraphQL 스키마 정의

  1. Django 모델 정의
  2. DjangoObjectType 정의 (참고)
    • 필드 설정: fields, exclude
  3. 쿼리 정의 (참고)
  4. 뮤테이션 정의 (참고)
  5. 스키마 정의
    1
    
      schema = graphene.Schema(query=Query, mutation=Mutation)
    

4. URL 구성

1
2
3
4
5
6
7
8
   # urls.py
   from django.urls import path
   from graphene_django.views import GraphQLView
   from django.views.decorators.csrf import csrf_exempt

   urlpatterns = [
       path("graphql/", csrf_exempt(GraphQLView.as_view(graphiql=True))),
   ]
  • GraphQLView는 GraphQL API의 엔드포인트를 제공
  • graphiql=True는 GraphQL을 탐색하고 테스트할 수 있는 웹 기반 IDE인 GraphiQL 인터페이스를 활성화함

예시

schema.py

기본 쿼리

1
2
3
4
5
6
7
8
9
class RecipeType(DjangoObjectType):
  class Meta:
      model = Recipe

class Query(graphene.ObjectType):
    all_recipes = graphene.List(RecipeType)

    def resolve_all_recipes(self, info, **kwargs):
        return Recipe.objects.all()

등록된 모든 레시피 조회

조건 쿼리

1
2
3
4
5
6
7
8
9
10
11
12
class UserType(DjangoObjectType):
    class Meta:
        model = User
        
class Query(graphene.ObjectType):
    all_users = graphene.List(UserType, is_superuser=graphene.Boolean())

    def resolve_all_users(self, info, is_superuser=None):
        queryset = User.objects.all()
        if is_superuser is not None:
            queryset = queryset.filter(is_superuser=is_superuser)
        return queryset

superuser가 아닌 유저 조회

1
2
3
4
5
class Query(graphene.ObjectType):
    recipes_by_title = graphene.List(RecipeType, title=graphene.String())

    def resolve_recipes_by_title(self, info, title):
        return Recipe.objects.filter(title__icontains=title)

제목에 “chicken” 포함된 레시피 조회

뮤테이션

생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CreateUser(graphene.Mutation):
    class Arguments:
        username = graphene.String(required=True)
        email = graphene.String(required=True)
        password = graphene.String(required=True)

    user = graphene.Field(UserType)

    def mutate(self, info, username, email, password):
        user = User.objects.create_user(username=username, email=email, password=password)
        return CreateUser(user=user)

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()

유저 생성

수정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class UpdateRecipe(graphene.Mutation):
    class Arguments:
        id = graphene.Int(required=True)
        new_title = graphene.String()

    recipe = graphene.Field(RecipeType)

    def mutate(self, info, id, new_title):
        recipe = Recipe.objects.get(id=id)
        recipe.title = new_title
        recipe.save()
        return UpdateRecipe(recipe=recipe)
    

class Mutation(graphene.ObjectType):
    update_recipe = UpdateRecipe.Field()

제목 변경 전

제목 변경 후

This post is licensed under CC BY 4.0 by the author.