Registering types for interfaces
See original GitHub issueBefore I go too far down this rabbithole(I learn from my mistakes), thought I’d ask if there is anything special I need to do to register types that are only used from an interface?
My scenario:
class DjangoObjectInterfaceMeta(DjangoObjectTypeMeta, InterfaceMeta):
pass
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()
shipping_destinations = graphene.List(ShippingDestinationType)
payment_methods = graphene.List(PaymentMethodType)
class Meta:
abstract = True
@classmethod
def _resolve_type(cls, schema, instance, *args):
if instance.is_anonymous():
return AnonymousUserType.internal_type(schema)
return UserType.internal_type(schema)
class AnonymousUserType(Customer):
pass
class UserType(six.with_metaclass(DjangoObjectInterfaceMeta, Customer)):
name = graphene.String()
class Meta:
model = User
only_fields = ['id', 'email', 'name', 'first_name', 'last_name', 'phone', 'receive_notifications',
'receive_update_emails', 'shipping_destinations', 'payment_methods']
def resolve_name(self, args, info):
return self.get_full_name()
def resolve_shipping_destinations(self, args, info):
return self.shipping_destinations.all()
def resolve_payment_methods(self, args, info):
return self.payment_methods.all()
When I am logged in, I get the user type returned fine. When I am not logged in, I get a Runtime Object type \"AnonymousUserType\" is not a possible type for \"Customer\".
returned. That is not true. If I modify graphene to pass types down to the init of GraphQLSchema
then it works for anonymous(but breaks other things).
Both of these types get registered to my schema with a register
call. I suspect this is because UserType is used elsewhere in my schema and so gets an additional something setup for it, while AnonymousUserType is just used here.
Anyone have any pointers?
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (5 by maintainers)
Top GitHub Comments
Hey @alexche8, here is a gist of the code I used to make it work: https://gist.github.com/danielfarrell/87561b8930d917f339aaff0ea8245231
It means that the
AnonymousUserType
is not registered in the schema, as there is no direct pointer from a field to it, and therefore the only available ObjectType forCustomer
isUserType
.You can solve by decorating the class, like:
Hope it helps! Please reopen the issue if not.