View Source File Uploads

Absinthe provides a unique pattern to support file uploads in conjunction with normal GraphQL field arguments as part of the absinthe_plug package.


The following schema includes a mutation field that accepts multiple uploaded files as arguments (:users and :metadata):

defmodule MyAppWeb.Schema do
  use Absinthe.Schema

  # Important: Needed to use the `:upload` type
  import_types Absinthe.Plug.Types

  mutation do
    field :upload_file, :string do
      arg :users, non_null(:upload)
      arg :metadata, :upload

      resolve fn args, _ ->
        args.users # this is a `%Plug.Upload{}` struct.

        {:ok, "success"}

To send a mutation that includes a file upload, you need to use the multipart/form-data content type. For example, using cURL:

$ curl -X POST \
-F query="mutation { uploadFile(users: \"users_csv\", metadata: \"metadata_json\")}" \
-F users_csv=@users.csv \
-F metadata_json=@metadata.json \

Note how there is a correspondence between the value of the :users argument and the -F option indicating the associated file.

By treating uploads as regular arguments we get all the usual GraphQL argument benefits (such as validation and documentation)---which we wouldn't get if we were merely putting them in the context as in other implementations.

Integration with Client-side GraphQL Frameworks