Last active
February 27, 2020 15:20
-
-
Save bicknest/760628ef98c3ce9ac788442c2bd2886b to your computer and use it in GitHub Desktop.
Creating a graphQL schema using GraphQL and django models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import graphene | |
from graphene_django.types import DjangoObjectType | |
from core.models import Profile | |
class ProfileType(DjangoObjectType): | |
class Meta: | |
model = Profile | |
class ProfileMutation(graphene.Mutation): | |
class Arguments: | |
# The input arguments for this mutation | |
name = graphene.String(required=True) | |
id = graphene.ID() | |
profile = graphene.Field(ProfileType) | |
def mutate(self, info, name, id): | |
profile = Profile.objects.get(uuid=uuid) | |
profile.name = name | |
profile.save() | |
return ProfileMutation(profile=profile) | |
class Query(object): | |
all_profiles = graphene.List(ProfileType) | |
profile = graphene.Field(ProfileType, id=graphene.String()) | |
def resolve_all_profiles(self, info, **kwargs): | |
return Profile.objects.all() | |
def resolve_profile(self, info, uuid): | |
return Profile.objects.get(id=id) | |
class Mutation(graphene.ObjectType): | |
update_profile_name = ProfileMutation.Field() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beautiful! I love it.