Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StructBlock's rawValue returning dictionary instead of string #349

Open
estyxx opened this issue Jul 27, 2023 · 0 comments
Open

StructBlock's rawValue returning dictionary instead of string #349

estyxx opened this issue Jul 27, 2023 · 0 comments

Comments

@estyxx
Copy link
Contributor

estyxx commented Jul 27, 2023

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>"
          }
        ]
      }
    ]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant