How to unit test absinthe API that accepts file upload as input

I found out in the following absinthe documentation https://hexdocs.pm/absinthe/Absinthe.html that absinthe has a run/3 function that allows you to run a GraphQL API from an elixir function or IEx as follows:

"""
query GetItemById($id: ID) {
  item(id: $id) {
    name
  }
}
"""
|> Absinthe.run(App.Schema, variables: %{"id" => params[:item_id]})

Knowing that absinthe has provided this run/3 function, I have decided to utilised it primarily for creating unit tests for my API. It has worked quite nicely for me however not until I tried to create a unit test that accepts an Upload type input to upload an image or any other binary file. An example of such API would be as follows

mutation UploadFile($file: File!) {
  uploadFile(file: $file) {
    id
    fileName
  }
}

where uploadFile is defined as follows using absinthe

field :upload_file, :some_output do
   arg(:file, non_null(:upload))
   resolve(&SomeResolver.some_function/2)
end

How could I use the Absinthe.run/3 function to pass a binary file to the mutation? I initially thought I could do this

"""
mutation UploadFile($file: File!) {
   uploadFile(file: $file) {
      id
      fileName
   }
}
"""
|> Absinthe.run(App.Schema, variables: %{"file => File.read!("path/to/file.ext")})

but no luck for me so far. I am not sure if this is even possible for now. Any help on this would be appreciated :slight_smile:

3 Likes