Created
June 4, 2017 15:38
-
-
Save danielfarrell/87561b8930d917f339aaff0ea8245231 to your computer and use it in GitHub Desktop.
Graphene Interface Example
This file contains hidden or 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 | |
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