Commit 94c2b462 authored by Nick Thomas's avatar Nick Thomas

Merge branch 'ac-accelerate-graphql' into 'master'

Ensure right order of processing multipart requests

See merge request gitlab-org/gitlab-ee!16461
parents a25bd893 4a97c182
Rails.application.configure do |config|
config.middleware.use(Gitlab::Middleware::Multipart)
# ApolloUploadServer::Middleware expects to find uploaded files ready to use
config.middleware.insert_before(ApolloUploadServer::Middleware, Gitlab::Middleware::Multipart)
end
# The Gitlab::Middleware::Multipart middleware inserts instances of our
......
......@@ -4,6 +4,7 @@ require "spec_helper"
describe "uploading designs" do
include GraphqlHelpers
include DesignManagementTestHelpers
include WorkhorseHelpers
let(:current_user) { create(:user) }
let(:issue) { create(:issue) }
......@@ -34,12 +35,24 @@ describe "uploading designs" do
expect(graphql_errors).to be_present
end
it 'succeeds' do
it 'succeeds (backward compatibility)' do
post_graphql_mutation(mutation, current_user: current_user)
expect(graphql_errors).not_to be_present
end
it 'succeeds' do
file_path_in_params = ['designManagementUploadInput', 'files', 0]
params = mutation_to_apollo_uploads_param(mutation, files: [file_path_in_params])
workhorse_post_with_file(api('/', current_user, version: 'graphql'),
params: params,
file_key: '1'
)
expect(graphql_errors).not_to be_present
end
it "responds with the created designs" do
post_graphql_mutation(mutation, current_user: current_user)
......
......@@ -172,6 +172,31 @@ module GraphqlHelpers
post_graphql(mutation.query, current_user: current_user, variables: mutation.variables)
end
# this implements GraphQL multipart request v2
# https://github.com/jaydenseric/graphql-multipart-request-spec/tree/v2.0.0-alpha.2
# this is simplified and do not support file deduplication
def mutation_to_apollo_uploads_param(mutation, files: [])
operations = { 'query' => mutation.query, 'variables' => mutation.variables }
map = {}
extracted_files = {}
files.each_with_index do |file_path, idx|
apollo_idx = (idx + 1).to_s
parent_dig_path = file_path[0..-2]
file_key = file_path[-1]
parent = operations['variables']
parent = parent.dig(*parent_dig_path) unless parent_dig_path.empty?
extracted_files[apollo_idx] = parent[file_key]
parent[file_key] = nil
map[apollo_idx] = ["variables.#{file_path.join('.')}"]
end
{ operations: operations.to_json, map: map.to_json }.merge(extracted_files)
end
# Raises an error if no data is found
def graphql_data
json_response['data'] || (raise NoData, graphql_errors)
......
......@@ -29,7 +29,8 @@ module WorkhorseHelpers
post(url,
params: workhorse_params,
headers: workhorse_rewritten_fields_header('file' => file.path)
headers: workhorse_rewritten_fields_header(file_key => file.path),
env: { 'CONTENT_TYPE' => 'multipart/form-data' }
)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment