Skip to content

Instantly share code, notes, and snippets.

@bicknest
Last active February 27, 2020 15:20
Show Gist options
  • Save bicknest/760628ef98c3ce9ac788442c2bd2886b to your computer and use it in GitHub Desktop.
Save bicknest/760628ef98c3ce9ac788442c2bd2886b to your computer and use it in GitHub Desktop.
Creating a graphQL schema using GraphQL and django models
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()
@tgevaert
Copy link

Beautiful! I love it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment