I would like to extend DjangoObjectType to add some custom Meta attributes. Before 2.0 I did this through a custom DjangoObjectTypeMeta, which was ugly but effective. After 2.0 it looks like this is done with "__init_subclass_with_meta__". However DjangoObjectTypeOptions is hard-coded in "DjangoObjectType.__init_subclass_with_meta__" as "_meta = DjangoObjectTypeOptions(cls)"
Is there a clean way to extend the Meta? Could we add a options_class kwarg that defaults to DjangoObjectTypeOptions but could be overridden if needed?
What about an interface? That should let you extend meta for a DjangoObjectType. It works the same way for DjangoObjectType as for graphene.ObjectType.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
One solution we figured out is simply be inheriting from DjangoObjectType and add a Meta with abstract = True. Then you can override the __init_subclass_with_meta__ classmethod but you need to make sure you do everything DjangoObjectType does in its __init_subclass_with_meta__ (copy the code from the DjangoObjectType, which isn't nice but actually you need do customize it, so it is they only way).
At the end make sure to call super(DjangoObjectType,...) and not super(YourChildClass, ...) because we wanna skip the DjangoObjectType and use directly the ObjectType.
Some might ask, why not deriving from ObjectType instead? The reason for this is that DjangoObjectType is hardcoded in the registry so we can't use any DjangoConnection without the DjangoObjectType as base class.
This is how it should generally look like:
class CwaDjangoObjectType(DjangoObjectType):
class Meta:
abstract = True
@classmethod
def __init_subclass_with_meta__(...):
# your custom code and the code you need from DjangoObjectType
# the following contains no type, we want to skip the actuall implementation from
# DjangoObjectType as we already did the things here and don't want to do it twice
super(DjangoObjectType, cls).__init_subclass_with_meta__(
_meta=_meta, interfaces=interfaces, **options
)
Most helpful comment
One solution we figured out is simply be inheriting from
DjangoObjectTypeand add a Meta with abstract = True. Then you can override the__init_subclass_with_meta__classmethod but you need to make sure you do everything DjangoObjectType does in its__init_subclass_with_meta__(copy the code from the DjangoObjectType, which isn't nice but actually you need do customize it, so it is they only way).At the end make sure to call
super(DjangoObjectType,...)and notsuper(YourChildClass, ...)because we wanna skip the DjangoObjectType and use directly the ObjectType.Some might ask, why not deriving from ObjectType instead? The reason for this is that DjangoObjectType is hardcoded in the registry so we can't use any DjangoConnection without the DjangoObjectType as base class.
This is how it should generally look like: