GrappleGQL / gatsby-source-wagtail

Plugin for sourcing Gatsby data from Wagtail CMS
17 stars 12 forks source link

Querying `blocks` field of a `StructBlock` in a `StreamField` breaks query #23

Closed tbrlpld closed 4 years ago

tbrlpld commented 4 years ago

Hi,

I just ran into an error when trying to query the blocks field of a StructBlock in a StreamField.

My BlogPage Wagtail model contains a StreamField like this:

class BlogPage(HeadlessPreviewMixin, Page):
    ...
    freeformbody = StreamField(
        [
            ...
            ('person', blocks.StructBlock([
                ('first_name', blocks.CharBlock()),
                ('last_name', blocks.CharBlock()),
                ('photo', ImageChooserBlock(required=False)),
                ('biography', blocks.TextBlock()),
            ], icon='user')),
            ...
        ],
        blank=True,
    )

    graphql_fields = [
        ...
        gpm.GraphQLStreamfield('freeformbody'),
        ...
    ]

A query for the StructBlock without the blocks field will work fine and I get the expected response.

query ($slug: String) {
  wagtail {
    page(slug: $slug) {
      ... on BlogPage {
        freeformbody {
          ... on StructBlock {
            id
            rawValue
          }
        }
      }
    }
  }
}

But when I add the blocks field

query ($slug: String) {
  wagtail {
    page(slug: $slug) {
      ... on BlogPage {
        freeformbody {
          ... on StructBlock {
            id
            rawValue
            blocks {
              id
            }
          }
        }
      }
    }
  }
}

then I get the following error:

{
  "errors": [
    {
      "message": "int() argument must be a string, a bytes-like object or a number, not 'Image'",
      "locations": [
        {
          "line": 9,
          "column": 13
        }
      ],
      "path": [
        "wagtail",
        "page",
        "freeformbody",
        21,
        "blocks"
      ],
      "stack": [
        "Error: int() argument must be a string, a bytes-like object or a number, not 'Image'",
        "    at defaultMergedResolver (<...>node_modules/graphql-tools/dist/stitching/defaultMergedResolver.js:15:36)",
        "    at wrappedTracingResolver (<...>node_modules/gatsby/dist/schema/resolvers.js:412:14)",
        "    at wrappedTracingResolver (<...>node_modules/gatsby/dist/schema/resolvers.js:412:14)",
        "    at wrappedTracingResolver (<...>node_modules/gatsby/dist/schema/resolvers.js:412:14)",
        "    at wrappedTracingResolver (<...>node_modules/gatsby/dist/schema/resolvers.js:412:14)",
        "    at resolveFieldValueOrError (<...>node_modules/graphql/execution/execute.js:467:18)",
        "    at resolveField (<...>node_modules/graphql/execution/execute.js:434:16)",
        "    at executeFields (<...>node_modules/graphql/execution/execute.js:275:18)",
        "    at collectAndExecuteSubfields (<...>node_modules/graphql/execution/execute.js:713:10)",
        "    at completeObjectValue (<...>node_modules/graphql/execution/execute.js:703:10)",
        "    at completeAbstractValue (<...>node_modules/graphql/execution/execute.js:660:10)",
        "    at completeValue (<...>node_modules/graphql/execution/execute.js:585:12)",
        "    at completeValueCatchingError (<...>node_modules/graphql/execution/execute.js:495:19)",
        "    at <...>node_modules/graphql/execution/execute.js:618:25",
        "    at Array.forEach (<anonymous>)",
        "    at forEach (<...>node_modules/iterall/index.js:83:25)"
      ]
    }
  ],
  "data": {
    ...
  }
}

It seems like the issue is related to the handling of the ImageChooserBlock that is on field in the StructBlock.

Update: I just confirmed it. When I remove the ImageChooserBlock from the StructBlock definition, then I can query the blocks field successfully.

Update: Just added the field back in and it worked as long as no image was defined for the field in the backend. Once an image is selected, then it breaks.

tbrlpld commented 4 years ago

Sorry, just realized this is already coming from the backend. I'll open the issue in the Grapple repo and close this one.

tbrlpld commented 4 years ago

Guess this is related to https://github.com/GrappleGQL/wagtail-grapple/issues/48 and https://github.com/GrappleGQL/wagtail-grapple/pull/54