torchbox/wagtail-grapple

The StreamField callback functionality fails when you have a field named after a base block attribute

zerolab opened this issue · 0 comments

Given

class MyBlock(StructBlock):
    label = CharBlock()
    # ...

    graphql_fields = [
        GraphQLString("label"),
    ]

you get

File "grapple/apps.py", line 15, in ready
  import_apps()
File "grapple/actions.py", line 89, in import_apps
  node_type = build_streamfield_type(
File "grapple/actions.py", line 468, in build_streamfield_type
  custom_cls_resolver(cls=cls, graphql_field=item) or streamfield_resolver
File "grapple/actions.py", line 408, in custom_cls_resolver
  if isinstance(getattr(type(cls()), graphql_field.field_source), property):
AttributeError: type object 'MyBlock' has no attribute 'label'

because Wagtail's Block class has a label attribute (ref)

workaround:

class MyBlock(StructBlock):
    label = CharBlock()
    # ...

    graphql_fields = [
        GraphQLString("label", source="get_label"),
    ]

    def get_label(self, *args, **kwargs):
      return self.label