torchbox / wagtail-grapple

A Wagtail app that makes building GraphQL endpoints a breeze!

Home Page:https://wagtail-grapple.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

StructBlock's rawValue returning dictionary instead of string

estyxx opened this issue · comments

When using the rawValue field of a GraphQLStreamfield in a query, it fails for StructBlocks.

Specifically, when querying for rawValue on a GraphQLStreamfield which maps to a StreamField populated with instances of a StructBlock, it returns a dictionary instead of a string. This behavior is inconsistent with what occurs when rawValue is queried on a GraphQLStreamfield which maps to a StreamField populated with instances of simpler blocks (e.g., CharBlock, RichTextBlock), where it correctly returns a string as expected.

Steps to reproduce:

  1. Define a StructBlock with a CharBlock field and register it with @register_streamfield_block. For example:
@register_streamfield_block
class LinkStreamBlock(blocks.StructBlock):
    link_title = blocks.CharBlock(required=True)

    class Meta:
        icon = "link"
        label = "Link"
  1. Use this StructBlock in a StreamField of a Page model and register it for GraphQL querying:
@grapple_helpers.register_query_field("blogPage")
class BlogPage(Page):
    links = StreamField(
        [
            ("item", LinkStreamBlock()),
        ],
        blank=True,
        use_json_field=True,
        default=[],
    )
    body = StreamField(
        [
            ("heading", blocks.CharBlock(classname="full title")),
            ("paragraph", blocks.RichTextBlock()),
        ],
        use_json_field=True,
        blank=True,
    )
    graphql_fields = [
        GraphQLStreamfield("links"),
        GraphQLStreamfield("body"),
    ]
  1. Execute a GraphQL query which requests rawValue of the links and body field:
query {
 pages {
  id
  
  ...on BlogPage {
    links {
      rawValue
    }
    body {
      rawValue
    }
  }
}

The response for links field is an error indicating that a dictionary was received when a string was expected, while the body field works as expected:

{
  "errors": [
    {
      "message": "String cannot represent value: {'link_title': 'hello world'}",
      "locations": [
        {
          "line": 7,
          "column": 7
        }
      ],
      "path": [
        "pages",
        1,
        "links",
        0,
        "rawValue"
      ]
    }
  ],
  "data": {
    "pages": [
      {
        "id": "4",
        "links": [
          null
        ],
        "body": [
          {
            "field": "heading",
            "rawValue": "Heading"
          },
          {
            "field": "paragraph",
            "rawValue": "<p data-block-key=\"2f60z\">This is a paragraph</p>"
          }
        ]
      }
    ]
  }
}