-
In Rails, I have a bunch of models that have an associated But generally, the client doesn't have much use for the In GraphQL, I'd like the Type objects for those models to be able to return I am imagining those model Types inheriting from a module Types::Models
class User < Types::Models::BaseWithImage
field :id, Integer, null: false
field :image_id, Integer, null: true
...
# somehow tell Types::Models::BaseWithImage that the @image_field_name is "image_id" in this Type
# maybe @image_field_name = "image_id" module Types::Models
class Herbarium < Types::Models::BaseWithImage
field :id, Integer, null: false
field :thumb_image_id, Integer, null: true
...
# somehow tell Types::Models::BaseWithImage that the @image_field_name is "thumb_image_id" in this Type
# maybe @image_field_name = "thumb_image_id" module Types::Models
class BaseWithImage < Types::BaseObject
field :img_src_thumb, String, null: true do
Image.url(:thumbnail, object.send(@image_field_name.to_sym))
end
field :img_src_sm, String, null: true do
Image.url(:small, object.send(@image_field_name.to_sym))
end
field :img_src_med, String, null: true do
Image.url(:medium, object.send(@image_field_name.to_sym))
end
field :img_src_lg, String, null: true do
Image.url(:large, object.send(@image_field_name.to_sym))
end
field :img_src_huge, String, null: true do
Image.url(:huge, object.send(@image_field_name.to_sym))
end
field :img_src_full, String, null: true do
Image.url(:full, object.send(@image_field_name.to_sym))
end and then somehow (this code does not work! plus I'm realizing Functions are deprecated, this is just for illustration) class Functions::ImageUrlFunction < GraphQL::Function
attr_accessor :size, :image_field_name
description "Retrieve image URL by size and image_field_name"
def initialize(image_field_name, size)
@size = size
@image_field_name = image_field_name
end
def call(_obj, _args, _ctx)
Image.url(@size.to_sym, object.send(@image_field_name.to_sym))
end that would resolve the call to My problem is I don't understand how to pass the name of the field up to that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ah! Of course, it's an Interface i want. So much easier than i thought! module Types::Models
class UserType < Types::BaseObject
implements Types::ImageUrls module Types::ImageUrls
include Types::BaseInterface
description "Fields generating urls from image_id, thumb_image_id, etc"
field :img_src_thumb, String, null: true
field :img_src_sm, String, null: true
field :img_src_med, String, null: true
field :img_src_lg, String, null: true
field :img_src_huge, String, null: true
field :img_src_full, String, null: true
def db_image_id
case object
when ::User
object.image_id
when ::Image
object.id
when ::Observation, ::GlossaryTerm
object.thumb_image_id
else
raise("Unexpected Object: #{object.inspect}")
end
end
def img_src_thumb
Image.url(:thumbnail, db_image_id)
end
def img_src_sm
Image.url(:small, db_image_id)
end
def img_src_med
Image.url(:medium, db_image_id)
end
def img_src_lg
Image.url(:large, db_image_id)
end
def img_src_huge
Image.url(:huge, db_image_id)
end
def img_src_full
Image.url(:full_size, db_image_id)
end
# Optional, see below
definition_methods do
# Optional: if this method is defined, it overrides `Schema.resolve_type`
def resolve_type(object, context)
# ...
end
end
end |
Beta Was this translation helpful? Give feedback.
Ah! Of course, it's an Interface i want. So much easier than i thought!