Skip to content

Instantly share code, notes, and snippets.

@vahidabdi
Last active March 5, 2020 22:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vahidabdi/d515b0163dc9c4a0b473105aaaf9ebcc to your computer and use it in GitHub Desktop.
Save vahidabdi/d515b0163dc9c4a0b473105aaaf9ebcc to your computer and use it in GitHub Desktop.
Arc Uploader resolver for Absinthe Graphql
defmodule MyApp.Schema.ArcResolver do
defmacro __using__([uploader: uploader]) do
quote do
import unquote(__MODULE__), only: [
get_file: 2
]
@__arc_upload unquote(uploader)
end
end
defmacro get_file(field, version) do
quote do
unquote(__MODULE__).get_file(@__arc_upload, unquote(field), unquote(version))
end
end
def get_file(uploader, field, version) do
fn parent, _, _ ->
res = apply(uploader, :url, [{parent.picture, parent}, version])
{:ok, res}
end
end
end
defmodule MyApp.Context.Service do
use Ecto.Schema
use Arc.Ecto.Schema
alias MyApp.Picture
schema "services" do
field :picture, Picture.Type
end
end
defmodule MyApp.Schema.Types do
use Absinthe.Schema.Notation
use MyApp.Schema.ArcResolver, uploader: MyApp.Picture
object :service do
field :thumb, :string, resolve: get_file(:picture, :thumb)
field :original, :string, resolve: get_file(:picture, :original)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment