Graphql files upload testing

Graphql is not very convenient when it comes to uploading files, but it is possible, for example, gem apollo-upload-server-ruby  is often used for this purpose.

To check file uploads with apollo-upload-server-ruby, I recommend using Altair GraphQL Client it allows you to easily upload files, unlike more well-known competitors such as insomnia.

Unit-testing file uploads with apollo-upload-server-ruby is not very obvious either, by examining the source code of the apollo-upload-server-ruby gem I found that in order for apollo-upload-server-ruby to properly respond to an uploaded file, the following code must be used:


 RSpec.describe Mutations::YourMutation, type: :mutation do

  subject { YourSchema.execute(query, variables: graphql_variables).as_json }





let(:uploaded_file) { Rack::Test::UploadedFile.new('spec/fixtures/files/test.png') }

let(:graphql_variables) { { image: image } }



  let(:image) do

    ApolloUploadServer::Wrappers::UploadedFile.new(

      ActionDispatch::Http::UploadedFile.new(

        tempfile: uploaded_file, filename: File.basename(uploaded_file),

        type: 'image/png'

      )

    )

  end



  def query

    <<~GRAPHQL

       mutation YourMutation($image: Upload!) {

        images(input: { image: $image }) {

          image {

            id

          }

        }

      }



    GRAPHQL

  end

Happy Testing!

Do you have a project for us?Get a free quote