Skip to content

Instantly share code, notes, and snippets.

@danielfarrell
Created June 4, 2017 15:38
Show Gist options
  • Save danielfarrell/87561b8930d917f339aaff0ea8245231 to your computer and use it in GitHub Desktop.
Save danielfarrell/87561b8930d917f339aaff0ea8245231 to your computer and use it in GitHub Desktop.
Graphene Interface Example
import graphene
from graphene_django.types import DjangoObjectType
class Customer(graphene.Interface):
id = graphene.Int()
email = graphene.String()
name = graphene.String()
first_name = graphene.String()
last_name = graphene.String()
phone = graphene.String()
receive_notifications = graphene.Boolean()
receive_update_emails = graphene.Boolean()
@classmethod
def resolve_type(cls, instance, context, info):
if instance.is_anonymous():
return AnonymousUserType
return UserType
class AnonymousUserType(graphene.ObjectType):
class Meta:
interfaces = (Customer,)
def resolve_shipping_destinations(self, args, context, info):
return []
def resolve_payment_methods(self, args, context, info):
return []
class UserType(DjangoObjectType):
name = graphene.String()
class Meta:
model = User
interfaces = (Customer,)
only_fields = ['id', 'email', 'name', 'first_name', 'last_name', 'phone', 'receive_notifications',
'receive_update_emails']
def resolve_name(self, args, context, info):
return self.get_full_name()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment