question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Registering types for interfaces

See original GitHub issue

Before 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:closed
  • Created 7 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
danielfarrellcommented, Jun 4, 2017

Hey @alexche8, here is a gist of the code I used to make it work: https://gist.github.com/danielfarrell/87561b8930d917f339aaff0ea8245231

1reaction
syrusakbarycommented, May 18, 2016

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 for Customer is UserType.

You can solve by decorating the class, like:

@schema.register
class AnonymousUserType(Customer):
    pass

Hope it helps! Please reopen the issue if not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Registering a Type as an Interface and as Self With ASP. ...
NET Core's built-in service collection supported registering types as a specific interface, and AsSelf() , much like Autofac supports?
Read more >
Register all implemented interfaces by a type with Simple ...
Register all implemented interfaces by a type with Simple Injector ... you can register all types derived from an interface and all the...
Read more >
How to register a service with multiple interfaces in ASP. ...
In this post I describe how to register a concrete class with multiple public interfaces in the Microsoft.Extensions.
Read more >
Registering Interfaces - Win32 apps
Servers register their interfaces by calling the RpcServerRegisterIf function. Complex server programs often support more than one interface.
Read more >
Registering all types as generic interfaces in assembly in ...
Basically, you would get current assembly, select all types implementing IRepository<> and register them as interface they implement, which is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found