Graphene
Python용 GraphQL 프레임워크. Django 모델을 GraphQL 스키마로 매핑하여, 단일 엔드포인트에서 클라이언트가 원하는 데이터만 요청할 수 있게 한다.
핵심 구조
graphene vs graphene-django
| 패키지 | 역할 |
|---|---|
graphene | Python GraphQL 코어 (ObjectType, Mutation, Schema) |
graphene-django | Django ORM 연동 (DjangoObjectType, DjangoFilterConnectionField) |
ObjectType — 스키마 정의
import graphene
from graphene_django import DjangoObjectType
from .models import Product
class ProductType(DjangoObjectType):
class Meta:
model = Product
fields = ("id", "name", "price", "category")DjangoObjectType을 상속하면 Django 모델 필드가 GraphQL 필드로 자동 변환된다.
Query — 조회
class Query(graphene.ObjectType):
products = graphene.List(ProductType)
product = graphene.Field(ProductType, id=graphene.ID(required=True))
def resolve_products(self, info):
return Product.objects.all()
def resolve_product(self, info, id):
return Product.objects.get(pk=id)Mutation — 변경
class CreateProduct(graphene.Mutation):
class Arguments:
name = graphene.String(required=True)
price = graphene.Decimal(required=True)
product = graphene.Field(ProductType)
def mutate(self, info, name, price):
product = Product.objects.create(name=name, price=price)
return CreateProduct(product=product)Schema 조립
schema = graphene.Schema(query=Query, mutation=Mutation)Saleor에서의 활용
Saleor는 Graphene 기반으로 전체 API를 구축한다.
saleor/graphql/api.py— Root Schema 정의saleor/graphql/product/— 상품 도메인 타입, 뮤테이션- DataLoader 패턴으로 N+1 문제 해결
BaseMutation을 확장하여 권한 검사, 에러 처리 표준화
자세한 분석은 Saleor Core - GraphQL, Settings, Dependencies 분석 참조.
DRF와의 비교
Django REST Framework (DRF)와의 상세 비교는 해당 문서의 “Graphene과의 비교” 섹션 참조.
핵심 차이: DRF는 리소스 중심(엔드포인트가 많고 응답 형태가 고정), Graphene은 쿼리 중심(엔드포인트 하나, 클라이언트가 형태를 결정).
관련 문서
- Django — Graphene의 기반 프레임워크
- Django REST Framework (DRF) — Django에서 REST API를 쓰는 대안
- Saleor Core - GraphQL, Settings, Dependencies 분석 — Graphene 실전 사용 사례
- Saleor vs Medusa 비교 분석 — GraphQL-first 아키텍처 비교