diff options
Diffstat (limited to 'spec/support/helpers/graphql_helpers.rb')
-rw-r--r-- | spec/support/helpers/graphql_helpers.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb index beb346b2855..4d2ad165fd6 100644 --- a/spec/support/helpers/graphql_helpers.rb +++ b/spec/support/helpers/graphql_helpers.rb @@ -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) |