GraphQL
GraphQL
Section titled “GraphQL”Djangoでは、Grapheneを使用してGraphQL APIを実装できます。
Grapheneの設定
Section titled “Grapheneの設定”依存関係の追加
Section titled “依存関係の追加”pip install graphene-djangoスキーマの定義
Section titled “スキーマの定義”import graphenefrom graphene_django import DjangoObjectTypefrom .models import User
class UserType(DjangoObjectType): class Meta: model = User fields = ("id", "name", "email")
class Query(graphene.ObjectType): user = graphene.Field(UserType, id=graphene.Int())
def resolve_user(self, info, id): return User.objects.get(id=id)
schema = graphene.Schema(query=Query)