torchbox / wagtail-grapple

A Wagtail app that makes building GraphQL endpoints a breeze!
https://wagtail-grapple.readthedocs.io/en/latest/
Other
152 stars 57 forks source link

Support for custom image models #168

Closed jnlsn closed 3 years ago

jnlsn commented 3 years ago

Hello 👋 I ran into my first hurdle with this excellent project and can't seem to find any documentation on it. Does Grapple support custom image models?

When I attempt to query an image field which implements a custom model I get an error like...

"Expected value of type \"ImageObjectType\" but got: FooImage."
zerolab commented 3 years ago

Hey @jayarnielsen,

Grapple does support custom image models - https://github.com/GrappleGQL/wagtail-grapple/blob/main/grapple/types/images.py#L139

it is difficult to say what is going on without having more examples of how you're using Grapple

jnlsn commented 3 years ago

Thanks for the quick response! I have a pretty straightforward custom image model:

class MyImage(AbstractImage):
    original_format = models.CharField(max_length=10, blank=True)
    admin_form_fields = WagtailImage.admin_form_fields

    def save(self, *args, **kwargs):
        with self.get_willow_image() as willow:
            self.original_format = willow.format_name
        return super().save(*args, **kwargs)

class MyRendition(AbstractRendition):
    image = models.ForeignKey(
        MyImage,
        on_delete=models.CASCADE,
        related_name='renditions',
    )

    class Meta:
        unique_together = (
            ('image', 'filter_spec', 'focal_point_key'),
        )

and am setting it up according to the Wagtail docs:

WAGTAILIMAGES_IMAGE_MODEL = 'core.MyImage'

But when i query for an image...

{
  images {
    url
  }
}

I get this response:

{
  "errors": [
    {
      "message": "Expected value of type \"ImageObjectType\" but got: MyImage.",
      "locations": [
        {
          "line": 31,
          "column": 3
        }
      ]
    }
  ],
  "data": null
}

If I disable the custom image class I can query for images just fine.

zerolab commented 3 years ago

How odd 🤔

The Grapple example app uses a custom image model: https://github.com/GrappleGQL/wagtail-grapple/blob/main/example/images/models.py https://github.com/GrappleGQL/wagtail-grapple/blob/main/example/example/settings/base.py#L162

Is your core app added to GRAPPLE_APPS in settings? (e.g. https://github.com/GrappleGQL/wagtail-grapple/blob/main/example/example/settings/base.py#L174)

jnlsn commented 3 years ago

OMG! 🤦 That was exactly it. I'd be happy to contribute to the documentation to hopefully clear that up for others in the future.

zerolab commented 3 years ago

Yes please. All improvements are welcome!