'Django graphene: add field resolver with saving of the type inherited from the model
I have a node inherited from the model:
class OrderModel(models.Model):
FAILED = "failed"
REQUIRES_PAYMENT_METHOD = "requires_payment_method"
ORDER_STATUSES = (
(FAILED, FAILED),
(REQUIRES_PAYMENT_METHOD, REQUIRES_PAYMENT_METHOD),
)
STATUSES_MAP_FOR_NOT_ADMINS = {
REQUIRES_PAYMENT_METHOD: FAILED,
}
status = models.CharField(_('status'), choices=ORDER_STATUSES, default=NEW_ORDER, max_length=255)
class Meta(object):
db_table = 'order'
verbose_name = _('order')
verbose_name_plural = _('orders')
class OrderNode(PrimaryKeyMixin, DjangoObjectType):
status = graphene.String
def resolve_status(self, info):
if info.context.user.is_admin:
return self.status
return self.STATUSES_MAP_FOR_NOT_ADMINS.get(self.status, self.status)
class Meta:
model = OrderModel
filter_fields = GrantedOrderFilter.Meta.fields
interfaces = (relay.Node, )
I want to add a custom status resolver to map statuses displayed for the users. But with the current implementation, I'm losing the typing for the status field. Is there any way I can save typing generated from the model and add the custom resolver?
Solution 1:[1]
Found the next solution:
OrderStatus = graphene.Enum('OrderStatus', {
status: status for status, _ in GrantedOrder.ORDER_STATUSES
})
class OrderNode(PrimaryKeyMixin, DjangoObjectType):
status = OrderStatus
But maybe there are more efficient solutions?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | domanskyi |
