Colibri
Colibri

Reputation: 1195

How to use image_url helper in GraphQL?

In the Action View the helper image_url returns this:

http://localhost:3010/assets/no-image-hash.jpg

For GraphQL in the config/initializers directory I put the following file:

module GraphQL
  module Define
    class DefinedObjectProxy
      include ActionView::Helpers::AssetUrlHelper
      include ImagesHelper
    end
  end
end

But it didn't help.

Inside the GraphQL type, the field returns the following:

"image": "/images/no-image.jpg",

And it should be this:

http://localhost:3010/assets/no-image-hash.jpg

Why doesn't it work and what am I doing wrong?

Upvotes: 1

Views: 508

Answers (2)

Levi
Levi

Reputation: 45

When using image_url outside the standard request context you will need to access it with ActionController::Base.helpers.image_path("example.png") to get the image hash.

Further discussion/ other solutions here: https://github.com/rails/rails/issues/10051

Upvotes: 1

Alex.U
Alex.U

Reputation: 1701

I believe you need to use image_url(). Have a glimpse at the docs:

Computes the full URL to an image asset. This will use image_path internally, so most of their behaviors will be the same. Since image_url is based on asset_url method you can set :host options. If :host options is set, it overwrites global config.action_controller.asset_host setting.

image_url "edit.png", host: "http://stage.example.com" # => http://stage.example.com/assets/edit.png

Upvotes: 0

Related Questions